xxxxxxxxxx
for (const [key, value] of Object.entries(object)) {
console.log(key, value);
}
xxxxxxxxxx
var p = {
"p1": "value1",
"p2": "value2",
"p3": "value3"
};
// for-in
for (var key in p) {
if (p.hasOwnProperty(key)) {
console.log(key + " -> " + p[key]);
}
}
// for-of with Object.keys()
for (var key of Object.keys(p)) {
console.log(key + " -> " + p[key])
}
// Object.entries()
for (let [key, value] of Object.entries(p)) {
console.log(`${key}: ${value}`);
}
xxxxxxxxxx
var obj = {a: 1, b: 2, c: 3};
for (const prop in obj) {
console.log(`obj.${prop} = ${obj[prop]}`);
}
// Salida:
// "obj.a = 1"
// "obj.b = 2"
// "obj.c = 3"
xxxxxxxxxx
var obj = {
first: "John",
last: "Doe"
};
//
// Visit non-inherited enumerable keys
//
Object.keys(obj).forEach(function(key) {
console.log(key, obj[key]);
});
xxxxxxxxxx
/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.
//////////////////////////////////////////////////////////
xxxxxxxxxx
var person = {"name":"Rasel", age:26};
for (var property in person) {
console.log(person[property]);
}
xxxxxxxxxx
var p = {
"p1": "value1",
"p2": "value2",
"p3": "value3"
};
for (var key in p) {
if (p.hasOwnProperty(key)) {
console.log(key + " -> " + p[key]);
}
}
xxxxxxxxxx
// data
let errors ={
"email":"Password is required",
"phonenumber":"Password is required",
"password":"Password is required"
}
//how to loop object
Object.entries(errors).forEach(error => {
console.log(error)
})
//results
['email', 'Password is required']
['phonenumber', 'Password is required']
['password', 'Password is required']
xxxxxxxxxx
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}
xxxxxxxxxx
let myOldCruiser = {make: 'kawasaki', model: 'LTD 454', year: 1987}
let objectPrinter = (object) => {
for (const key in object) {
if (object.hasOwnProperty(key)) {
console.log(`${key}: ${object[key]}`);
}
}
};
objectPrinter(myOldCruiser)
// Output: make: kawasaki
// model: LTD 454
// year: 1987