# Help in Merging two objects with condition

**URL:** <https://discourse.nodered.org/t/help-in-merging-two-objects-with-condition/95231>\
**Category:** General\
**Tags:** join\
**Created:** [8 February 2025 04:54 UTC](https://discourse.nodered.org/t/help-in-merging-two-objects-with-condition/95231 "2025-02-08T04:54:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [8 February 2025 04:54 UTC](https://discourse.nodered.org/t/help-in-merging-two-objects-with-condition/95231/1 "2025-02-08T04:54:52Z")

</div>

I have two arrays with three objects each.(this three may change to any number, but will always be equal numbers). i want to join these arrays into one array, but the objects should be merged with a condition of date being equal (the data is cleansed and they are unique records, no duplicate dates).  
I am unable to get it going. Eventually I need to calculate OEE, with mulitplying the respective date's uptime with speed for the day, and get the target and later do rest of the calculation.

```auto
[{"id":"54ac643a30582248","type":"group","z":"ffc4fcc1699193f0","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["213d8c5915e8033d","3c25666c402d375e","7dc31930824f26be","b965fba69473527c","4550b71af69b8836","93feac8f6c4ce81e","bb872ecada3c6e6a"],"x":594,"y":3619,"w":852,"h":162},{"id":"213d8c5915e8033d","type":"inject","z":"ffc4fcc1699193f0","g":"54ac643a30582248","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":700,"y":3700,"wires":[["3c25666c402d375e","7dc31930824f26be"]]},{"id":"3c25666c402d375e","type":"change","z":"ffc4fcc1699193f0","g":"54ac643a30582248","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"Date\":\"01-02-2025, Sat\",\"Shift_A\":\"-\",\"Shift_B\":\"-\",\"Shift_C\":\"612\",\"Total_Day_F\":\"612\",\"Total_Day\":\"612\",\"UP\":15,\"DN\":1425,\"TOT\":1440,\"AVAIL\":\"1.04\"},{\"Date\":\"02-02-2025, Sun\",\"Shift_A\":\"18,134\",\"Shift_B\":\"45,234\",\"Shift_C\":\"50,440\",\"Total_Day_F\":\"113,808\",\"Total_Day\":\"113808\",\"UP\":1061,\"DN\":379,\"TOT\":1440,\"AVAIL\":\"73.68\"},{\"Date\":\"03-02-2025, Mon\",\"Shift_A\":\"47,537\",\"Shift_B\":\"44,625\",\"Shift_C\":\"45,811\",\"Total_Day_F\":\"137,973\",\"Total_Day\":\"137973\",\"UP\":1270,\"DN\":169,\"TOT\":1439,\"AVAIL\":\"88.26\"}]","tot":"json"},{"t":"set","p":"topic","pt":"msg","to":"availability","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":930,"y":3660,"wires":[["b965fba69473527c","93feac8f6c4ce81e"]]},{"id":"7dc31930824f26be","type":"change","z":"ffc4fcc1699193f0","g":"54ac643a30582248","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"Date\":\"01-02-2025, Sat\",\"Speed\":50,\"Occurs\":3},{\"Date\":\"02-02-2025, Sun\",\"Speed\":110,\"Occurs\":995},{\"Date\":\"03-02-2025, Mon\",\"Speed\":110,\"Occurs\":1214}]","tot":"json"},{"t":"set","p":"topic","pt":"msg","to":"speed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":3740,"wires":[["4550b71af69b8836","93feac8f6c4ce81e"]]},{"id":"b965fba69473527c","type":"debug","z":"ffc4fcc1699193f0","g":"54ac643a30582248","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1120,"y":3660,"wires":[]},{"id":"4550b71af69b8836","type":"debug","z":"ffc4fcc1699193f0","g":"54ac643a30582248","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":3740,"wires":[]},{"id":"93feac8f6c4ce81e","type":"join","z":"ffc4fcc1699193f0","g":"54ac643a30582248","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1170,"y":3700,"wires":[["bb872ecada3c6e6a"]]},{"id":"bb872ecada3c6e6a","type":"debug","z":"ffc4fcc1699193f0","g":"54ac643a30582248","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1340,"y":3700,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![yusufscelik](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yusufscelik/32/98689_2.png) [@yusufscelik](https://discourse.nodered.org/u/yusufscelik)\
**Post date:** [8 February 2025 07:51 UTC](https://discourse.nodered.org/t/help-in-merging-two-objects-with-condition/95231/2 "2025-02-08T07:51:13Z")

</div>

Have you tried join node?

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [8 February 2025 08:02 UTC](https://discourse.nodered.org/t/help-in-merging-two-objects-with-condition/95231/3 "2025-02-08T08:02:07Z")

</div>

yes, i am unable to join the particular object into the array with the correct date.

---

<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:** [8 February 2025 08:18 UTC](https://discourse.nodered.org/t/help-in-merging-two-objects-with-condition/95231/4 "2025-02-08T08:18:58Z")

</div>

After the join node use a function node with something like:

```auto
const input = msg.payload

const dataMap = {};
input.speed.forEach(item => {
    dataMap[item.Date] = item;
});

input.availability.forEach(availabilityItem => {
    const speedItem = dataMap[availabilityItem.Date];
    if (speedItem) {
        Object.assign(availabilityItem, speedItem);
    }
});

msg.payload = input.availability;
return msg;

```

note the correct terminology is 'merge' for this use case.

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [8 February 2025 08:27 UTC](https://discourse.nodered.org/t/help-in-merging-two-objects-with-condition/95231/5 "2025-02-08T08:27:00Z")

</div>

BINGO!!

```auto
[{"id":"213d8c5915e8033d","type":"inject","z":"ffc4fcc1699193f0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":700,"y":3700,"wires":[["3c25666c402d375e","7dc31930824f26be"]]},{"id":"3c25666c402d375e","type":"change","z":"ffc4fcc1699193f0","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"Date\":\"01-02-2025, Sat\",\"Shift_A\":\"-\",\"Shift_B\":\"-\",\"Shift_C\":\"612\",\"Total_Day_F\":\"612\",\"Total_Day\":\"612\",\"UP\":15,\"DN\":1425,\"TOT\":1440,\"AVAIL\":\"1.04\"},{\"Date\":\"02-02-2025, Sun\",\"Shift_A\":\"18,134\",\"Shift_B\":\"45,234\",\"Shift_C\":\"50,440\",\"Total_Day_F\":\"113,808\",\"Total_Day\":\"113808\",\"UP\":1061,\"DN\":379,\"TOT\":1440,\"AVAIL\":\"73.68\"},{\"Date\":\"03-02-2025, Mon\",\"Shift_A\":\"47,537\",\"Shift_B\":\"44,625\",\"Shift_C\":\"45,811\",\"Total_Day_F\":\"137,973\",\"Total_Day\":\"137973\",\"UP\":1270,\"DN\":169,\"TOT\":1439,\"AVAIL\":\"88.26\"}]","tot":"json"},{"t":"set","p":"topic","pt":"msg","to":"availability","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":930,"y":3660,"wires":[["b965fba69473527c","93feac8f6c4ce81e"]]},{"id":"7dc31930824f26be","type":"change","z":"ffc4fcc1699193f0","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"Date\":\"01-02-2025, Sat\",\"Speed\":50,\"Occurs\":3},{\"Date\":\"02-02-2025, Sun\",\"Speed\":110,\"Occurs\":995},{\"Date\":\"03-02-2025, Mon\",\"Speed\":110,\"Occurs\":1214}]","tot":"json"},{"t":"set","p":"topic","pt":"msg","to":"speed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":3740,"wires":[["4550b71af69b8836","93feac8f6c4ce81e"]]},{"id":"b965fba69473527c","type":"debug","z":"ffc4fcc1699193f0","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1120,"y":3660,"wires":[]},{"id":"4550b71af69b8836","type":"debug","z":"ffc4fcc1699193f0","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":3740,"wires":[]},{"id":"93feac8f6c4ce81e","type":"join","z":"ffc4fcc1699193f0","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1170,"y":3700,"wires":[["bb872ecada3c6e6a","73c20df8098b2bad"]]},{"id":"bb872ecada3c6e6a","type":"debug","z":"ffc4fcc1699193f0","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1460,"y":3700,"wires":[]},{"id":"73c20df8098b2bad","type":"function","z":"ffc4fcc1699193f0","name":"function 6","func":"const input = msg.payload\n\nconst dataMap = {};\ninput.speed.forEach(item => {\n dataMap[item.Date] = item;\n});\n\ninput.availability.forEach(availabilityItem => {\n const speedItem = dataMap[availabilityItem.Date];\n if (speedItem) {\n Object.assign(availabilityItem, speedItem);\n }\n});\n\nmsg.payload = input.availability;\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1340,"y":3640,"wires":[["f3bc39c01f63b374"]]},{"id":"f3bc39c01f63b374","type":"debug","z":"ffc4fcc1699193f0","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1500,"y":3640,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [8 February 2025 08:31 UTC](https://discourse.nodered.org/t/help-in-merging-two-objects-with-condition/95231/6 "2025-02-08T08:31:25Z")

</div>

In a change node apply this expression to the joined output

```auto
($$.payload.*{Date:$merge($)}).*

```

Or without a join and all inline

```auto
[{"id":"213d8c5915e8033d","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":5700,"wires":[["3c25666c402d375e"]]},{"id":"3c25666c402d375e","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"availability","pt":"msg","to":"[{\"Date\":\"01-02-2025, Sat\",\"Shift_A\":\"-\",\"Shift_B\":\"-\",\"Shift_C\":\"612\",\"Total_Day_F\":\"612\",\"Total_Day\":\"612\",\"UP\":15,\"DN\":1425,\"TOT\":1440,\"AVAIL\":\"1.04\"},{\"Date\":\"02-02-2025, Sun\",\"Shift_A\":\"18,134\",\"Shift_B\":\"45,234\",\"Shift_C\":\"50,440\",\"Total_Day_F\":\"113,808\",\"Total_Day\":\"113808\",\"UP\":1061,\"DN\":379,\"TOT\":1440,\"AVAIL\":\"73.68\"},{\"Date\":\"03-02-2025, Mon\",\"Shift_A\":\"47,537\",\"Shift_B\":\"44,625\",\"Shift_C\":\"45,811\",\"Total_Day_F\":\"137,973\",\"Total_Day\":\"137973\",\"UP\":1270,\"DN\":169,\"TOT\":1439,\"AVAIL\":\"88.26\"}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":5700,"wires":[["7dc31930824f26be"]]},{"id":"7dc31930824f26be","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"speed","pt":"msg","to":"[{\"Date\":\"01-02-2025, Sat\",\"Speed\":50,\"Occurs\":3},{\"Date\":\"02-02-2025, Sun\",\"Speed\":110,\"Occurs\":995},{\"Date\":\"03-02-2025, Mon\",\"Speed\":110,\"Occurs\":1214}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":5700,"wires":[["4550b71af69b8836","9cd66f51587ef366"]]},{"id":"4550b71af69b8836","type":"debug","z":"d1395164b4eec73e","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":760,"y":5620,"wires":[]},{"id":"9cd66f51587ef366","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"([$$.availability, $$.speed]{Date:$merge($)}).*","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":5780,"wires":[["bb872ecada3c6e6a"]]},{"id":"bb872ecada3c6e6a","type":"debug","z":"d1395164b4eec73e","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":840,"y":5700,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [8 February 2025 08:39 UTC](https://discourse.nodered.org/t/help-in-merging-two-objects-with-condition/95231/7 "2025-02-08T08:39:23Z")

</div>

That works too.! Pity I can't mark 2 solutions in one thread.

---

<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:** [22 February 2025 08:40 UTC](https://discourse.nodered.org/t/help-in-merging-two-objects-with-condition/95231/8 "2025-02-22T08:40:00Z")

</div>

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