![]() |
VOOZH | about |
JavaScript offers built-in methods that simplify working with object data. Object.entries(), Object.keys(), and Object.values() help extract and manipulate object properties efficiently.
The Object.entries() method returns an array of a given object's own enumerable property [key, value] pairs.
Output:
[ [ 'name', 'Prakash' ], [ 'age', 99 ], [ 'city', 'Mumbai' ] ]As you can see, Object.entries(obj) returns an array of arrays, where each inner array contains a key-value pair from the object.
The Object.keys() method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would.
Output:
[ 'name', 'age', 'city' ]Object.keys(obj) returns an array containing the keys of the object.
The Object.values() method returns an array of a given object's own enumerable property values, in the same order as provided by a for...in loop.
Output:
[ 'Prakash', 99, 'Mumbai' ]Object.values(obj) returns an array containing the values of the object.
Suppose you have an object with numerical values, and you want to find the sum of these values. Here's how you can do it:
20
You can check if a property exists in an object using the in operator:
true true false
You can use a for...in loop to iterate over the keys of an object: