Object.keys() is a static method in JavaScript that is used to retrieve an array of the enumerable property names of an object. It takes an object as an argument and returns an array containing the names of all the enumerable properties of that object.
Advantages of Object.keys():
- Directly Returns Array: It efficiently returns an array of property names, ready for iteration.
- Concise and Readable: The syntax is clear and straightforward, making code easier to understand and maintain. It explicitly indicates that you are interested in the keys of an object, making the intention of the code clear
- Compatible with Array Methods: The returned array can be used with various array methods like map, filter, sort, forEach etc., expanding its functionality to perform various operations on the object properties
- Selective Property Iteration: Object.keys() allows you to iterate over specific properties of an object by choosing which keys to include in the resulting array. This is particularly useful when you don't want to iterate over all properties of an object.
Overall, Object.keys() is a versatile and convenient method for extracting keys from an object, providing a clean and effective way to iterate over its properties.
Here are some examples:
let school = { name: 'GeeksforGeeks', location: 'India', students: 5000, teachers: 100 }; let keys = Object.keys(school); // ['name', 'location', 'students', 'teachers'] keys.forEach(key => { console.log(key + ': ' + school[key]); }); // Output: // name: GeeksforGeeks // location: India // students: 5000 // teachers: 100
Suppose you have an object that represents a school, with properties such as name, location, students, and teachers. You can use Object.keys() to get an array of the property names, and then use the forEach() method to print them and their values.
// Define a sample vehicle object const vehicle = { type: 'Car', brand: 'Toyota', model: 'Camry', year: 2022, color: 'Silver', }; // Use Object.keys() to get an array of property names const properties = Object.keys(vehicle); // Iterate over the properties and log their values properties.forEach(property => { console.log(`${property}: ${vehicle[property]}`); });
In this example, we have a simple vehicle object with properties like type, brand, model, year, and color. The Object.keys() method is used to get an array of property names, and then we iterate over these names to access and log the corresponding values.
const airships = { name: "Hindenburg", length: 245, crew: 40, engines: 4 }; const keys = Object.keys(airships); for (const key of keys) { console.log(`${key}: ${airships[key]}`); } //Filtering Properties const filteredKeys = keys.filter(key => key !== "crew"); console.log(filteredKeys); // Output: ["name", "length", "engines"] //Sorting Properties const sortedKeys = keys.sort(); console.log(sortedKeys); // Output: ["crew", "engines", "length", "name"]
Explanation:
- Retrieve property names into keys array.
- Loop through keys, accessing each property's value using airships[key].
- Filter keys array to exclude "crew" property. Output: Array with remaining property names
- Sort keys array alphabetically