# Map() through array of objects and do math on them

**URL:** <https://discourse.nodered.org/t/map-through-array-of-objects-and-do-math-on-them/68433>\
**Category:** General\
**Created:** [30 September 2022 22:43 UTC](https://discourse.nodered.org/t/map-through-array-of-objects-and-do-math-on-them/68433 "2022-09-30T22:43:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![VinistoisR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinistoisr/32/5778_2.png) [@VinistoisR](https://discourse.nodered.org/u/VinistoisR)\
**Post date:** [30 September 2022 22:43 UTC](https://discourse.nodered.org/t/map-through-array-of-objects-and-do-math-on-them/68433/1 "2022-09-30T22:43:44Z")

</div>

I have this array of objects coming in on a msg.payload.

(the actual array has lots of other key:value pairs that I'm not interested in

```auto
[
{
  "id": "99999",
  "signal": "voltage1",
  "timeseries": [
       {
         "time": 1664553600000,
         "value": 2
       },
       {
         "time": 1664553700000,
         "value": 4
       }]
},
{
  "id": "99999",
  "signal": "voltage2",
  "timeseries": [
       {
         "time": 1664553600000,
         "value": 3
       },
       {
         "time": 1664553700000,
         "value": 1
       }]
},
{
  "id": "99999",
  "signal": "current",
  "timeseries": [
       {
         "time": 1664553600000,
         "value": 5
       },
       {
         "time": 1664553700000,
         "value": 4
       }]
}
]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/e/eeb0ab59550d5d4e9d2246657aac5403e9e1bd3f.png)

I want to match up the timestamps and do ("voltage1" + "voltage2") \* "current"

Then I want to output a new array that has the result for each timestamp.

I started with the map() function, and got here:

```auto
const input = msg.payload;

const output = input.map(signal => {
    const container = {};
    container["id"] = signal.id;
    container["signal"] = signal.signal;
    container["timeseries"] = signal.timeseries;
    return container;
});   

msg.payload = output;

return msg;

```

this does return me new arrays with only the information I'm interested in. But I'm a bit lost on how to do the math operations. I've found math.max() math.sum(), but nothing much more complex.

Anyone have pointers?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [1 October 2022 05:04 UTC](https://discourse.nodered.org/t/map-through-array-of-objects-and-do-math-on-them/68433/2 "2022-10-01T05:04:52Z")

</div>

Example function node:

```auto
// function to find signal and timestamp
const f = (key, t) => i.find(v => v.signal === key).timeseries.find(x => x.time === t).value

const i = msg.payload
const results = [] 

// get array all of timestamps - assuming voltage1 timestamp also exist for voltage2 and current
const timestamps = i.find(v => v.signal === 'voltage1').timeseries.map( k=>k.time ) 

// loop through all timestamps and push result to results array
timestamps.forEach(t => {

        const v1 = f('voltage1', t)
        const v2 = f('voltage2', t)
        const c = f('current', t)
        const result = (v1 + v2) * c 
        // key will be the timestamp
        results.push({ [t]: result })
})

msg.results = results
return msg;

```

output:

```auto
[{
    "1664553600000": 25
}, {
    "1664553700000": 20
}]

```

---

<div class="post-metadata">

**Author:** ![VinistoisR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinistoisr/32/5778_2.png) [@VinistoisR](https://discourse.nodered.org/u/VinistoisR)\
**Post date:** [3 October 2022 18:38 UTC](https://discourse.nodered.org/t/map-through-array-of-objects-and-do-math-on-them/68433/3 "2022-10-03T18:38:45Z")

</div>

@bakman2 thank you so much.

This is possibly the most helpful forum on the entire internet thanks to contributors like you. I just learned more about Javascript in 5 minutes than in the last 5 months. Big kudos!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [17 October 2022 18:39 UTC](https://discourse.nodered.org/t/map-through-array-of-objects-and-do-math-on-them/68433/4 "2022-10-17T18:39:30Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
