We can perform object inspection using the console.log()
command. It prints the object’s properties and values to the console.
Key takeaways
JavaScript object stores data as key-value pairs, allowing organized and flexible data management. To get keys, values, and entries, use the following methods:
Object.keys(obj)
: This retrieves an array of the object's keys.
Object.values(obj)
: This retrieves an array of the object's values.
Object.entries(obj)
: This retrieves an array of key-value pairs.
Reflect.ownKeys(obj)
: This retrieves an array of all keys, including both string and symbolic keys.
Object.getOwnPropertySymbols(obj)
: This retrieves an array of only symbolic keys of the object.These methods improve the ability to efficiently manage, access, and manipulate JavaScript objects.
JavaScript by Brendan Eich provides a variety of data types to store various types of data. Among them, JavaScript objects are used to store data collections in the form of key-value pairs. To get the JavaScript keys, values, and entries of a JavaScript object data type, we use the following method:
The Object.keys(obj)
method returns all the keys of the object as an array.
The Object.values(obj)
method returns all the values of the object as an array.
The Object.entries(obj)
method returns an array of the key-value pairs.
These methods give developers different ways to work with objects in JavaScript, which facilitates data manipulation and other operations in applications.
Let’s say we have an object named user
:
let user ={name : "Arya",age : 23,greet : () => "Hello",};
user
objectWe use the Object.keys()
method to retrieve the keys of the user
object:
let user ={name : "Arya",age : 23,greet : () => "Hello",};let keys = Object.keys(user);console.log(keys); //["name", "age", "greet"]
Object.keys()
returns the keys that are available in the object while calling the function—any new keys added after this are not updated.
let keys = Object.keys(user);keys; //["name", "age", "greet"]user.weight = 65;// weight key will not be added to keys arrayconsole.log(keys); //["name", "age", "greet"]
user
objectWe use the Object.values()
method to get the values of the user
object:
let values = Object.values(user);console.log(values); // ["Arya", 23, ƒ, 65]
user
objectWe use the Object.entries()
method to get the key-value pairs of the user
object:
let entries = Object.entries(user);console.log(entries);
We can use the Object.entries()
to get the user
object’s key-value pairs, and we can use array methods like map()
, filter()
, and reduce()
on the object.
map()
method with Object.entries()
In the following code, we process each pair using the map()
method, which iterates over each entry of user
, destructures each entry into key
and val
, and transforms them into a formatted string like "The ${key} is ${val}"
. This iteration results in an array of descriptive strings, which is then logged to the console, displaying "The name is Arya"
and "The age is 23"
.
let user ={name : "Arya",age : 23,};let entries = Object.entries(user)let data = entries.map( ([key, val] = entry) => {return `The ${key} is ${val}`;});console.log(data); // ["The name is Arya", "The age is 23"]
Note: When using the
Object.keys()
method, symbolic keys are not returned. Instead, we use theReflect.ownKeys(obj)
method call to return all keys. Or, we useObject.getOwnPropertySymbols
to get only the symbolic keys.
Haven’t found what you were looking for? Contact Us