gsiradze
gsiradze

Reputation: 4733

map object with properties not only string

I have this object

enter image description here

and I want to do something like this:

  Object.keys(changes).map(i => {
    activity.change = i
    this.addINDB(activity)
  })

But this map iterates over description and name as string. not these objects which I want to assign to my activity.change

What can I do?

Upvotes: 0

Views: 34

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074208

You can use Object.values instead of Object.keys. You'll need to polyfill it on even slightly out-of-date browsers, but it's polyfillable.

Also note that if you're not using the return value, map isn't the right tool for looping through the array. You'd want forEach (or a for-of loop as you're clearly using ES2015+).

Example with Object.values:

Object.values(changes).forEach(value => {
    this.addINDB(value);
});

or

for (const value of Object.values(changes)) {
    this.addINDB(value);
}

Or just get the value for the key inside your Object.keys callback:

Object.keys(changes).forEach(key => {
    this.addINDB(changes[key]);
});

or

for (const key of Object.keys(changes)) {
    this.addINDB(changes[key]);
}

Upvotes: 2

Related Questions