Answers for "looping through object js"

85

javascript loop through object

Object.entries(obj).forEach(
    ([key, value]) => console.log(key, value)
);
Posted by: Guest on February-15-2020
2

javascript for loop on object

/Example 1: Loop Through Object Using for...in
// program to loop through an object using for...in loop

const student = { 
    name: 'John',
    age: 20,
    hobbies: ['reading', 'games', 'coding'],
};

// using for...in
for (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:
}

/////////////////////////////////////////

/Example 2: Loop Through Object Using Object.entries and for...of
// program to loop through an object using for...in loop

const student = { 
    name: 'John',
    age: 20,
    hobbies: ['reading', 'games', 'coding'],
};

// using Object.entries
// using for...of loop
for (let [key, value] of Object.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.

//////////////////////////////////////////////////////////
Posted by: Guest on September-07-2021
0

javascript looping through object

const fruits = {
  apple: 28,
  orange: 17,
  pear: 54,
}

const entries = Object.entries(fruits)
console.log(entries)
// [
//   [apple, 28],
//   [orange, 17],
//   [pear, 54]
// ]
Posted by: Guest on May-19-2020
1

js loop through object

for (var key in validation_messages) {
    // skip loop if the property is from prototype
    if (!validation_messages.hasOwnProperty(key)) continue;

    var obj = validation_messages[key];
    for (var prop in obj) {
        // skip loop if the property is from prototype
        if (!obj.hasOwnProperty(prop)) continue;

        // your code
        alert(prop + " = " + obj[prop]);
    }
}
Posted by: Guest on July-16-2020

Code answers related to "looping through object js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language