Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do .forEach() for object in javascript? [duplicate]

I have a data structure similar to:

var object = {
  name: "a",
  number: "1",
  mission: ['a','b','c']
}

Now I want to update them into the database so I need to walk them.

I tried:

object.forEach(function(value, key, map){
  console.log('value: ' + value + ', key: ' + key + ', map:' + map);
});

And then the console report an error: object.forEach is not a function

like image 1000
Aero Wang Avatar asked Jul 20 '26 19:07

Aero Wang


1 Answers

Try this,

Object.keys(object).forEach(key=>{
    console.log(key ,object[key]);
})
like image 181
Anurag Awasthi Avatar answered Jul 23 '26 08:07

Anurag Awasthi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!