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
/Example1:LoopThroughObjectUsingfor...in// program to loop through an object using for...in loopconst student ={name:'John',age:20,hobbies:['reading','games','coding'],};// using for...infor(let key in student){let value;// get the value
value = student[key];console.log(key +" - "+ value);}/Output
name -John
age -20
hobbies -["reading","games","coding"]//If you want, you can only loop through the object's//own property by using the hasOwnProperty() method.if(student.hasOwnProperty(key)){++count:}//////////////////////////////////////////Example2:LoopThroughObjectUsingObject.entries and for...of// program to loop through an object using for...in loopconst student ={name:'John',age:20,hobbies:['reading','games','coding'],};// using Object.entries// using for...of loopfor(let[key, value]ofObject.entries(student)){console.log(key +" - "+ value);}/Output
name -John
age -20
hobbies -["reading","games","coding"]//In the above program, the object is looped using the//Object.entries() method and the for...of loop.//The Object.entries() method returns an array of a given object's key/value pairs.//The for...of loop is used to loop through an array.//////////////////////////////////////////////////////////
How to loop through an object in JavaScript with a for…in loop
const population ={male:4,female:93,others:10};// Iterate through the objectfor(const key in population){if(population.hasOwnProperty(key)){console.log(`${key}: ${population[key]}`);}}
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"});
const person ={first_name:'Monica',last_name:'Geller',phone:'915-996-9739',email:'monica37@gmail.com',street:'495 Grove Street',city:'New York',country:'USA',};for(const key in person){console.log(`${key} => ${person[key]}`);}
// 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"});