Help in Merging two objects with condition

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.

[{"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":[]}]

Have you tried join node?

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

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

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.

1 Like

BINGO!!

[{"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":[]}]

In a change node apply this expression to the joined output

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

Or without a join and all inline

[{"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":[]}]
2 Likes

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

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