Answers for "Which method do we use to loop an object?"

84

javascript loop through object

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

javascript loop object

let obj = {
  key1: "value1",
  key2: "value2",
  key3: "value3"
}

Object.keys(obj).forEach(key => {
    console.log(key, obj[key]);
});
// key1 value1
// key2 value2
// key3 value3

// using for in - same output as above
for (let key in obj) {
  let value = obj[key];
  console.log(key, value);
}
Posted by: Guest on May-04-2020
13

javascript loop through object

for (var property in object) {
  if (object.hasOwnProperty(property)) {
    // Do things here
  }
}
Posted by: Guest on September-09-2019
6

loop through object javascript

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}`);
}
Posted by: Guest on July-30-2020
0

loop through object js

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

//
//	Visit non-inherited enumerable keys
//
Object.keys(obj).forEach(function(key) {

  console.log(key, obj[key]);

});
Posted by: Guest on February-02-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

Code answers related to "Which method do we use to loop an object?"

Code answers related to "Javascript"

Browse Popular Code Answers by Language