In javascript object contain the key value pair properties and iterating over object is different from arrays . Objects can be iterated using for...in loops and Object.keys(), Object.values(), and Object.entries(). Let’s see how you can use each method:
1. using for...in method
const person = { name: 'John', age: 30, occupation: 'Engineer' }; for(let key in persons){ console.log(`${person[key]} : ${key}`) } //output // name: 'John', // age: 30, // occupation: 'Engineer'
2.Using Object.keys(): method
object.keys() is a javascript method which take object as argument and return array of keys
const person = { name: 'John', age: 30, occupation: 'Engineer' }; const Object_keys = Object.keys(person); console.log(Object_keys)// [ 'name', 'age', 'occupation']``````
we can use object.keys() to iterate over object
const person = { name: 'John', age: 30, occupation: 'Engineer' }; const Object_keys = Object.keys(person); //here first i have used Object_keys array which i got from Object.keys(person); for(let i = 0 ; iconst person = { name: 'John', age: 30, occupation: 'Engineer' }; const Object_keys = Object.keys(person); //here first i have used Object_keys array which i got from Object.keys(person); for(let i = 0 ; i 3.Using Object.entries(): Object.entries()
is a javascript method which take object as argument and return 2d array of key value pairconst person = { name: 'John', age: 30, occupation: 'Engineer' }; const Object_keys = Object.keys(person); //here first i have used Object_keys array which i got from Object.keys(person); for(let i = 0 ; iconst person = { name: 'John', age: 30, occupation: 'Engineer' }; const Object_keyValue = Object.entries(person); //output // [ , , ]
const person = { name: 'John', age: 30, occupation: 'Engineer' }; for (const [key, value] of Object.entries(person)) { console.log(`${key} : ${value}`); } //output // name: 'John', // age: 30, // occupation: 'Engineer'Object.entries()to iterate over object const person = { name: 'John', age: 30, occupation: 'Engineer' }; for (const [key, value] of Object.entries(person)) { console.log(`${key} : ${value}`); } //output // name: 'John', // age: 30, // occupation: 'Engineer'
4. Using Object.values():
const myObject = { prop1: 'value1', prop2: 'value2', prop3: 'value3' }; const values = Object.values(myObject); for (const value of values) { console.log(value); }Object.values() returns an array of an object's own enumerable property values. This can be useful if you're only interested in the values and not the keys.const myObject = { prop1: 'value1', prop2: 'value2', prop3: 'value3' }; const values = Object.values(myObject); for (const value of values) { console.log(value); }
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3