Skip to content
Advertisement

Sum all similar keys in an array with a random number of keys and array items

My Array (can have several items, and there can be multiple ‘keys’ other than ‘companyName’):

JavaScript

randomKey and randomKey2 are only examples. The key names can be anything and there can be 1 or more in each array item.

Expected results:

JavaScript

companyName: ‘X’ is a fixed value, and all of the items have this

Attempted:

JavaScript

But it leads to [{x: NaN}].

How can I fix this?

Advertisement

Answer

You can play around with this example

JavaScript

https://jsfiddle.net/gre6Ly1a/1/

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement