const object ={a:1,b:2,c:3};// method 1Object.entries(object).forEach(([key, value])=>{console.log(key, value)});//method 2for(const key in object){console.log(key, object[key])}// same output// a 1// b 2// c 3
// Looping through arrays created from Object.keysconst keys =Object.keys(fruits)for(const key of keys){console.log(key)}// Results:// apple// orange// pear
var p ={"p1":"value1","p2":"value2","p3":"value3"};for(var key in p){if(p.hasOwnProperty(key)){console.log(key +" -> "+ p[key]);}}Run code snippetHide results
for(const[fruit, count]of entries){console.log(`There are ${count}${fruit}s`)}// Result// There are 28 apples// There are 17 oranges// There are 54 pears
const student ={name:'Monica',class:7,age:12}// using for...infor(let key in student ){// display the propertiesconsole.log(`${key} => ${student[key]}`);}
const obj ={foo:'bar',baz:42};console.log(Object.entries(obj));// [ ['foo', 'bar'], ['baz', 42] ]// array like objectconst obj ={0:'a',1:'b',2:'c'};console.log(Object.entries(obj));// [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]// array like object with random key orderingconst anObj ={100:'a',2:'b',7:'c'};console.log(Object.entries(anObj));// [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]// getFoo is property which isn't enumerableconst myObj =Object.create({},{getFoo:{value(){returnthis.foo;}}});
myObj.foo='bar';console.log(Object.entries(myObj));// [ ['foo', 'bar'] ]// non-object argument will be coerced to an objectconsole.log(Object.entries('foo'));// [ ['0', 'f'], ['1', 'o'], ['2', 'o'] ]// returns an empty array for any primitive type except for strings (see the above example), since primitives have no own propertiesconsole.log(Object.entries(100));// [ ]// iterate through key-value gracefullyconst obj ={a:5,b:7,c:9};for(const[key, value]ofObject.entries(obj)){console.log(`${key}${value}`);// "a 5", "b 7", "c 9"}// Or, using array extrasObject.entries(obj).forEach(([key, value])=>{console.log(`${key}${value}`);// "a 5", "b 7", "c 9"});
// datalet errors ={"email":"Password is required","phonenumber":"Password is required","password":"Password is required"}//how to loop objectObject.entries(errors).forEach(error=>{console.log(error)})//results['email','Password is required']['phonenumber','Password is required']['password','Password is required']
const obj ={foo:'bar',baz:42};console.log(Object.entries(obj));// [ ['foo', 'bar'], ['baz', 42] ]// array like objectconst obj ={0:'a',1:'b',2:'c'};console.log(Object.entries(obj));// [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]// array like object with random key orderingconst anObj ={100:'a',2:'b',7:'c'};console.log(Object.entries(anObj));// [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]// getFoo is property which isn't enumerableconst myObj =Object.create({},{getFoo:{value(){returnthis.foo;}}});
myObj.foo='bar';console.log(Object.entries(myObj));// [ ['foo', 'bar'] ]// non-object argument will be coerced to an objectconsole.log(Object.entries('foo'));// [ ['0', 'f'], ['1', 'o'], ['2', 'o'] ]// returns an empty array for any primitive type except for strings (see the above example), since primitives have no own propertiesconsole.log(Object.entries(100));// [ ]// iterate through key-value gracefullyconst obj ={a:5,b:7,c:9};for(const[key, value]ofObject.entries(obj)){console.log(`${key}${value}`);// "a 5", "b 7", "c 9"}// Or, using array extrasObject.entries(obj).forEach(([key, value])=>{console.log(`${key}${value}`);// "a 5", "b 7", "c 9"});