The Object.keys()
method in JavaScript is used to extract all the keys or property names of an object and returns them as an array.
This method provides an efficient way to iterate over the properties of an object and perform operations based on their values.
Syntax for Object.keys()
method:
Object.keys(obj);
Here, obj
is the object whose property names need to be extracted.
The Object.keys()
method returns an array containing all the property names of the object obj
. Let’s take an example to understand the usage of the Object.keys()
method:
const person = {
name: "John Doe",
age: 30,
occupation: "Software Engineer",
};
const keys = Object.keys(person);
console.log(keys); // Output: ['name', 'age', 'occupation']
In this example, we have an object person
with three properties: name
, age
, and occupation
. We are using the Object.keys()
method to extract all the keys of this object and store them in an array keys
. The output of this code snippet will be an array ['name', 'age', 'occupation']
.
Let’s take another example where we can use the Object.keys()
method to iterate over the properties of an object and perform some operations based on their values:
const person = {
name: "John Doe",
age: 30,
occupation: "Software Engineer",
};
Object.keys(person).forEach(function (key) {
console.log(key + ": " + person[key]);
});
In this example, we are using the Object.keys()
method to extract all the keys of the person
object and iterate over them using the forEach()
method. Inside the forEach()
method, we are accessing the value of each property using the key and printing it to the console. The output of this code snippet will be:
name: John Doe
age: 30
occupation: Software Engineer
We can also use the Object.keys()
method to check if an object has a particular property or not:
const person = {
name: "John Doe",
age: 30,
occupation: "Software Engineer",
};
if (Object.keys(person).includes("age")) {
console.log("Age property is present");
} else {
console.log("Age property is not present");
}
In this example, we are using the Object.keys()
method to extract all the keys of the person
object and check if the array contains the string 'age'
or not. If the array contains the string 'age'
, then it means that the object has an age
property, and we print 'Age property is present'
to the console. Otherwise, we print 'Age property is not present'
.
The Object.keys()
method can also be used with reduce()
method to perform operations on the properties of an object:
const person = {
name: "John Doe",
age: 30,
occupation: "Software Engineer",
};
const totalChars = Object.keys(person).reduce(function (acc, key) {
return acc + person[key].length;
}, 0);
console.log(totalChars); // Output: 33
In this example, we are using the Object.keys()
method to extract all the keys of the person
object and then using the reduce()
method to calculate the total number of characters in the values of all the properties of the object. Inside the reduce()
method, we are accessing the value of each property using the key and adding its length to the accumulator acc
. The initial value of the accumulator is 0
. The output of this code snippet will be 33
, which is the total number of characters in the values of all the properties of the person
object.
The Object.keys()
method can also be used to create a copy of an object with a subset of its properties:
const person = {
name: "John Doe",
age: 30,
occupation: "Software Engineer",
};
const newPerson = Object.keys(person).reduce(function (obj, key) {
obj[key] = person[key];
return obj;
}, {});
console.log(newPerson); // Output: { name: 'John Doe', age: 30 }
In this example, we are using the Object.keys()
method to extract all the keys of the person
object and then using the reduce()
method to create a new object with only the name
and age
properties of the original person
object. Inside the reduce()
method, we are adding each property of the original person
object to the new object obj if its key is either 'name'
or 'age'
. The output of this code snippet will be an object { name: 'John Doe', age: 30 }
, which is a copy of the person object with only the name
and age
properties.
In conclusion, the Object.keys()
method in JavaScript is a useful method that allows us to extract all the keys or property names of an object and perform various operations on them.
Thank you for reading, and let’s have conversation with each other
Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.