javascript loop through object
snippet in javascript

javascript loop through object

user9653

Object.entries(obj).forEach(
    ([key, value]) => console.log(key, value)
);

javascript loop through object

user5914

for (var property in object) {
  if (object.hasOwnProperty(property)) {
    // Do things here
  }
}

javascript loop through object

user453

// object to loop through
let obj = { first: "John", last: "Doe" };

// loop through object and log each key and value pair
//ECMAScript 5
Object.keys(obj).forEach(function(key) {
    console.log(key, obj[key]);
});

//ECMAScript 6
for (const key of Object.keys(obj)) {
    console.log(key, obj[key]);
}

//ECMAScript 8
Object.entries(obj).forEach(
    ([key, value]) => console.log(key, value)
);

// OUTPUT
/*
   first John
   last Doe
*/

javascript loop through object

user8601

var person={
 	first_name:"johnny",
  	last_name: "johnson",
	phone:"703-3424-1111"
};
for (var property in person) {
  	console.log(property,":",person[property]);
}

javascript loop through object

user6424

var obj = { first: "John", last: "Doe" };

Object.keys(obj).forEach(function(key) {
    console.log(key, obj[key]);
});

javascript loop through object

user6839

Object.entries(obj).forEach(
    ([key, value]) => console.log(key, value)
);

// Loop using forEach
arr.forEach((item, index) => {
    // TODO
})

// Loop using for...of
for (const item of arr) {
    await something()
}

// Clone new array (not changing the old one)
const arr = [1,2,3]
const newArray = arr.map(item => item * 2)
console.log(newArray)

// Filter array by condition
const arr = [1,2,3,1]
const newArray = arr.filter(item => {
    if (item === 1) { return item }
})
console.log(newArray)

// Join array
const arr1 = [1,2,3]
const arr2 = [4,5,6]
const arr3 = [...arr1, ...arr2]
console.log(arr3)

// Get a property of object
const { email, address } = user
console.log(email, address)

// Copy object/array
const obj = { name: 'my name' }
const clone = { ...obj }
console.log(obj === clone)

javascript loop through object

user2463

for (var property in object) {
  if (object.hasOwnProperty(property)) {
    // Do things here
  }
}

javascript loop through object

user4123

var p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

for (var key in p) {
    if (p.hasOwnProperty(key)) {
        console.log(key + " -> " + p[key]);
    }
}

javascript loop through object

user1945

for (var property in object) {
  if (object.hasOwnProperty(property)) {
    // Do things here
  }
}

javascript loop through object

user1714

var person = {"name":"bob", age:45};

for (var property in person) {
   console.log(person[property]);
}