1let obj = {
2 key1: "value1",
3 key2: "value2",
4 key3: "value3",
5 key4: "value4",
6}
7Object.entries(obj).forEach(([key, value]) => {
8 console.log(key, value);
9});
1let person={
2 first_name:"johnny",
3 last_name: "johnson",
4 phone:"703-3424-1111"
5};
6for (let i in person) {
7 let t = person[i]
8 console.log(i,":",t);
9}
1for (let key in yourobject) {
2 if (yourobject.hasOwnProperty(key)) {
3 console.log(key, yourobject[key]);
4 }
5}
1const person = {
2 firstName: 'John',
3 lastName: 'Doe',
4 email: 'john.doe@example.com'
5};
6for (const [key, value] of Object.entries(person)) {
7 console.log(`${key}: ${value}`);
8}
9
10/*
11 Explanation -
12 Object.entries(person) returns: [["firstName","John"],["lastName","Doe"],["email","john.doe@example.com"]]
13 We can use an ES6 'for-of' loop to interate over it. Each iteration gets an
14 array where the 0th index is the property name and the 1st index is the
15 value. We can use ES6 array destructuring to directly extract the values
16 into separate variables.
17*/
1const obj = { 0: 'a', 1: 'b', 2: 'c' };
2console.log(Object.entries(obj)); // => [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
3
4Object.entries(obj).forEach((key, value) => {
5 console.log(key + ' ' + value);
6});