Answers for "javascript looping over objects"

85

javascript loop through object

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

javascript loop through object

// 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
*/
Posted by: Guest on May-12-2020
4

javascript loop through object values

var myObj = {foo: "bar", baz: "baz"};
Object.values(myObj).map((val) => {
console.log(val);
})
// "bar" "baz"
Posted by: Guest on June-11-2020
3

javascript loop through object

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)
Posted by: Guest on April-12-2020
0

javascript looping through object

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
Posted by: Guest on May-19-2020

Code answers related to "javascript looping over objects"

Code answers related to "Javascript"

Browse Popular Code Answers by Language