Hello!
Is there a way to delete duplicate entries from an array?
I hope someone can help me.
hello .. you can use a jsonata expression in a Change node
using the $distinct() array function
thanks for your answer
How do I build this exactly? Do you have an example?
Here you go
[{"id":"dd264254b20f70e8","type":"inject","z":"54efb553244c241f","name":"fake data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Heroes","payload":"[{\"name\":\"Molecule Man\",\"age\":29,\"secretIdentity\":\"Dan Jukes\",\"powers\":[\"Radiation resistance\",\"Turning tiny\",\"Radiation blast\"]},{\"name\":\"Molecule Man\",\"age\":29,\"secretIdentity\":\"Dan Jukes\",\"powers\":[\"Radiation resistance\",\"Turning tiny\",\"Radiation blast\"]},{\"name\":\"Molecule Man\",\"age\":29,\"secretIdentity\":\"Dan Jukes\",\"powers\":[\"Radiation resistance\",\"Turning tiny\",\"Radiation blast\"]},{\"name\":\"Molecule Man\",\"age\":29,\"secretIdentity\":\"Dan Jukes\",\"powers\":[\"Radiation resistance\",\"Turning tiny\",\"Radiation blast\"]},{\"name\":\"Madame Uppercut\",\"age\":39,\"secretIdentity\":\"Jane Wilson\",\"powers\":[\"Million tonne punch\",\"Damage resistance\",\"Superhuman reflexes\"]},{\"name\":\"Eternal Flame\",\"age\":1000000,\"secretIdentity\":\"Unknown\",\"powers\":[\"Immortality\",\"Heat Immunity\",\"Inferno\",\"Teleportation\",\"Interdimensional travel\"]}]","payloadType":"json","x":280,"y":2220,"wires":[["0b423c4865359f2f","b8de59a4d2bb94d8"]]},{"id":"962cc58d57493e06","type":"debug","z":"54efb553244c241f","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":720,"y":2220,"wires":[]},{"id":"0b423c4865359f2f","type":"change","z":"54efb553244c241f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$distinct(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":2220,"wires":[["962cc58d57493e06"]]},{"id":"b8de59a4d2bb94d8","type":"debug","z":"54efb553244c241f","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":430,"y":2160,"wires":[]}]
Debug 1 shows payload is array of 6 objects (where some are duplicates)
Debug 2 shows the result (3 unique objects) after the $distinct()
Hello!
Thanks for that.
and is it also possible to compare only one value?
for example
msg.payload.name
$distinct compares the array elements .. even if they are deep nested objects for differences
and returns the unique ones.
in your requirement you need to check only the name
and if the name is repeated many times but for example the age is different .. return what ? the first match ? some other condition ?
do you have some real sample data and how the output should be ?
I have an array with two values each.
and it should only be a comparison and if this occurs once in a while, it should then be deleted so that only one remains.
As in your example, only the name should be compared and should it occur again, the double should be completely awake.
Sounds like this could help
[{"id":"eb621e3ae7914a09","type":"inject","z":"6313bd35b2395547","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[[1,2],[\"a\",\"fwfe\"],[\"a\",\"2nd value\"]]","payloadType":"jsonata","x":230,"y":520,"wires":[["ad1d215a26a024ba"]]},{"id":"30b8ac740d4af8dc","type":"inject","z":"6313bd35b2395547","name":"reset","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":460,"wires":[["8cce1c633269dad8"]]},{"id":"06d062de94092412","type":"debug","z":"6313bd35b2395547","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1210,"y":520,"wires":[]},{"id":"ad1d215a26a024ba","type":"split","z":"6313bd35b2395547","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":410,"y":520,"wires":[["374ce67e472c8a12"]]},{"id":"518494695464316f","type":"inject","z":"6313bd35b2395547","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[[\"name\",\"val\"],[\"a\",\"3rd value\"]]","payloadType":"jsonata","x":230,"y":580,"wires":[["ad1d215a26a024ba"]]},{"id":"8cce1c633269dad8","type":"change","z":"6313bd35b2395547","name":"","rules":[{"t":"delete","p":"list","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":460,"wires":[[]]},{"id":"374ce67e472c8a12","type":"function","z":"6313bd35b2395547","name":"","func":"if (typeof msg.payload == \"object\" && Array.isArray(msg.payload) && msg.payload.length == 2) {\n let key = msg.payload[0];\n let value = msg.payload[1];\n let list = flow.get(\"list\") ?? {};\n if (!(key in list)) {\n list[key] = value;\n flow.set(\"list\", list);\n }\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":520,"wires":[["b1720a07c4c39ac8"]]},{"id":"b1720a07c4c39ac8","type":"change","z":"6313bd35b2395547","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"list","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":790,"y":520,"wires":[["5f55e78ce1953230","06d062de94092412"]]},{"id":"5f55e78ce1953230","type":"function","z":"6313bd35b2395547","name":"convert to array","func":"msg.payload = Object.entries(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1020,"y":580,"wires":[["06d062de94092412"]]}]
It takes before the split array of arrays, e.g. [ ["name","value"], ["a","fwfe"], ["a","2nd value"] ]
.
It collects all those tuples in flow.list
, if some name comes up again then it is discarded.
Press reset to delete flow.list to start fresh.
Node convert to array
is optional, just in case you need an array at the end again.
EDIT:
Just in case... some say map is faster than objects, the same in flow.map1 with a js Map:
[{"id":"94ca4e2013ba9dcc","type":"inject","z":"6313bd35b2395547","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[[1,2],[\"a\",\"fwfe\"],[\"a\",\"2nd value\"]]","payloadType":"jsonata","x":230,"y":760,"wires":[["6f8c34f6d9cc8c01"]]},{"id":"a1026b531409faba","type":"inject","z":"6313bd35b2395547","name":"reset","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":700,"wires":[["31530d0f2073d6b5"]]},{"id":"0c73b9a08bda54cd","type":"debug","z":"6313bd35b2395547","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1210,"y":760,"wires":[]},{"id":"6f8c34f6d9cc8c01","type":"split","z":"6313bd35b2395547","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":410,"y":760,"wires":[["2308e3a27ef36599"]]},{"id":"9c468332867198cb","type":"inject","z":"6313bd35b2395547","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[[\"name\",\"val\"],[\"a\",\"3rd value\"]]","payloadType":"jsonata","x":230,"y":820,"wires":[["6f8c34f6d9cc8c01"]]},{"id":"31530d0f2073d6b5","type":"change","z":"6313bd35b2395547","name":"","rules":[{"t":"delete","p":"map1","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":700,"wires":[[]]},{"id":"2308e3a27ef36599","type":"function","z":"6313bd35b2395547","name":"","func":"if (typeof msg.payload == \"object\" && Array.isArray(msg.payload) && msg.payload.length == 2) {\n let key = msg.payload[0];\n let value = msg.payload[1];\n let map1 = flow.get(\"map1\") ?? new Map();\n if (!(map1.has(key))) {\n map1.set(key, value);\n flow.set(\"map1\", map1);\n }\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":760,"wires":[["7343c92659e635e4"]]},{"id":"7343c92659e635e4","type":"change","z":"6313bd35b2395547","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"map1","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":790,"y":760,"wires":[["0c73b9a08bda54cd","ddb37998fa45b4f7"]]},{"id":"ddb37998fa45b4f7","type":"function","z":"6313bd35b2395547","name":"convert to array","func":"msg.payload = Array.from(msg.payload, ([name, value]) => ([ name, value ]));\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1020,"y":820,"wires":[["0c73b9a08bda54cd"]]}]
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.