Q:

javascript foreach get key and value

myObject ={a:1,b:2,c:3}

//es6
Object.entries(myObject).forEach(([key, value]) => {
  console.log(key , value); // key ,value
});

//es7
Object.keys(myObject).forEach(key => {
    console.log(key , myObject[key]) // key , value
})
29
const object1 = {
  a: 'somestring',
  b: 42
};

for (let [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}

// expected output:
// "a: somestring"
// "b: 42"
// order is not guaranteed
1
Object.keys(obj).forEach(function (key) {
   // do something with obj[key]
});
-1

New to Communities?

Join the community