How to split an array based on an element within the array?

Sorry, I've Googled and Googled but I can't find out how to do this. I've looked at threads that show functions, jsonata expressions, split nodes, change nodes - but because the array will have staff in different positions every time I can't work out how to split the payload the same way reliably.

So today, my own name is here:
payload[2].employee_name
but tomorrow it could be
payload[0].employee_name
or
payload[3].employee_name
So I'm just wondering how I can interrogate that part of the payload...when the key (is it called a key?) is subject to daily change!

thanks

Screenshot 2021-03-05 at 08.23.05

you can do this using standard JavaScript find function.

e.g...

function node

var users = msg.payload;
var find_id = "221244"; //you could pass this in msg.find_id if you wish!
var user = users.find(e => e.employee_id == find_id);
msg.payload = user;
return msg;

or less verbose...

msg.payload = msg.payload.find(e => e.employee_id == "221244");
return msg;

Sadly for both of those I get an error

either:

"TypeError: users.find is not a function"

"TypeError: msg.payload.find is not a function"

sorry!! Googled for half an hour to see if I could fix this myself without bothering you but found nothing :frowning:

I wrote that based on your screenshot that shows msg.payload is an array...
image

Put a debug BEFORE this new function - to see what is going into the function. It should be the same as your original screenshot (an array in msg.payload)

If you are not feeding an array of objects into the function as msg.payload, it wont work.

share your flow by selecting the relevant nodes and pressing CTRL+E then copy to clipboard then paste into a reply (press the </> code button before pasting code)

[{"id":"c9595213.416a8","type":"inject","z":"aea73244.52bb5","name":"","topic":"","payload":"{\"status\":1,\"data\":[{\"employee_id\":\"221244\",\"employee_eid\":\"\",\"employee_name\":\"Dane Boomatron\",\"employee_avatar\":\"0\",\"employee_avatar_url\":\"{\\\"large\\\":\\\"avatars\\\\/large_221244-2212441575277008.jpg\\\",\\\"small\\\":\\\"avatars\\\\/small_221244-2212441575277008.jpg\\\",\\\"bigger\\\":\\\"avatars\\\\/bigger_221244-2212441575277008.jpg\\\",\\\"full\\\":\\\"avatars\\\\/full_221244-2212441575277008.jpg\\\",\\\"profile\\\":\\\"avatars\\\\/profile_221244-2212441575277008.jpg\\\",\\\"forum\\\":\\\"avatars\\\\/forum_221244-2212441575277008.jpg\\\"}\",\"employee_avatar_url_small\":\"https://s3aws.com/uf.shiftping.com/avatars%441575277008.jpg\",\"schedule_id\":\"335645\",\"schedule_name\":\"Keyholder\",\"schedule_color\":\"1\",\"timeclock_id\":\"154394679\",\"clockin_time\":{\"id\":4327,\"month\":3,\"day\":4,\"wday\":4,\"year\":2021,\"weekday\":\"Thu\",\"mname\":\"Mar\",\"week\":9,\"dayid\":5,\"timeid\":37,\"timeid_exact\":37,\"formatted\":\"Mar 4, 2021\",\"iso8601\":\"2021-03-04T09:00:00+0000\",\"time\":\"09:00\",\"timestamp\":1614848400,\"hours\":9,\"minutes\":0,\"seconds\":0,\"date\":\"Mar 4, 2021\",\"day_of_year\":\"62\",\"week_adjusted\":9,\"sp_wday\":5,\"date_timestamp\":\"2021-03-04 09:00:00\"},\"shift_id\":null,\"shift_start\":null,\"shift_end\":null,\"is_on_break\":null,\"break\":[]}],\"metadata\":[],\"token\":\"7a1935d8bd7b7309367902dfba\",\"error\":null}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":360,"wires":[["a30e8c.3d725178","c717c9cf.98bca8","3fb8dc6e.db0b34"]]},{"id":"c717c9cf.98bca8","type":"function","z":"aea73244.52bb5","name":"","func":"msg.payload = msg.payload.find(e => e.employee_id == \"221244\");\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":520,"wires":[[]]},{"id":"3fb8dc6e.db0b34","type":"debug","z":"aea73244.52bb5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":340,"y":400,"wires":[]}]

Sorry totally agree it's now showing as an object rather than an array which I'm really confused about because I don't think I'm doing anything different or have changed anything...clearly I have but I don't know what?!

msg.payload = msg.payload.data.find(e => e.employee_id == "221244");
return msg;

now it works

You could also use JSONata in a change node to find object item

[{"id":"c9595213.416a8","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"name","v":"Dane Boomatron","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"status\":1,\"data\":[{\"employee_id\":\"221244\",\"employee_eid\":\"\",\"employee_name\":\"Dane Boomatron\",\"employee_avatar\":\"0\",\"employee_avatar_url\":\"{\\\"large\\\":\\\"avatars\\\\/large_221244-2212441575277008.jpg\\\",\\\"small\\\":\\\"avatars\\\\/small_221244-2212441575277008.jpg\\\",\\\"bigger\\\":\\\"avatars\\\\/bigger_221244-2212441575277008.jpg\\\",\\\"full\\\":\\\"avatars\\\\/full_221244-2212441575277008.jpg\\\",\\\"profile\\\":\\\"avatars\\\\/profile_221244-2212441575277008.jpg\\\",\\\"forum\\\":\\\"avatars\\\\/forum_221244-2212441575277008.jpg\\\"}\",\"employee_avatar_url_small\":\"https://s3aws.com/uf.shiftping.com/avatars%441575277008.jpg\",\"schedule_id\":\"335645\",\"schedule_name\":\"Keyholder\",\"schedule_color\":\"1\",\"timeclock_id\":\"154394679\",\"clockin_time\":{\"id\":4327,\"month\":3,\"day\":4,\"wday\":4,\"year\":2021,\"weekday\":\"Thu\",\"mname\":\"Mar\",\"week\":9,\"dayid\":5,\"timeid\":37,\"timeid_exact\":37,\"formatted\":\"Mar 4, 2021\",\"iso8601\":\"2021-03-04T09:00:00+0000\",\"time\":\"09:00\",\"timestamp\":1614848400,\"hours\":9,\"minutes\":0,\"seconds\":0,\"date\":\"Mar 4, 2021\",\"day_of_year\":\"62\",\"week_adjusted\":9,\"sp_wday\":5,\"date_timestamp\":\"2021-03-04 09:00:00\"},\"shift_id\":null,\"shift_start\":null,\"shift_end\":null,\"is_on_break\":null,\"break\":[]}],\"metadata\":[],\"token\":\"7a1935d8bd7b7309367902dfba\",\"error\":null}","payloadType":"json","x":190,"y":2360,"wires":[["c0ba082f.13824"]]},{"id":"c0ba082f.13824","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.data[employee_name=$$.name]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":2360,"wires":[["e26feb98.54a348"]]},{"id":"e26feb98.54a348","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":610,"y":2360,"wires":[]}]
payload.data[employee_name=$$.name]

where $$.name is the search term set by msg.name in inject node.

Thanks guys, really appreciate this!

Thank you - and I also noticed how carefully you'd obfuscated the token - I had also supplied a 'fake' token but it's really kind of you to notice that and consider that I might not have. Cheers!

1 Like

Thanks again - I've made leaps and bounds worth of progress since this morning, but hit a stumbling block with a particular individual - can't figure out why for this one individual my functions aren't working and are instead just dumping the entire payload?

[{"id":"e439a48a.6dfef8","type":"inject","z":"66862370.3ad20c","name":"","topic":"","payload":"{\"status\":1,\"data\":[{\"employee_id\":\"4157152\",\"employee_eid\":\"\",\"employee_name\":\"Todd\",\"employee_avatar\":\"0\",\"employee_avatar_url\":\"{\\\"large\\\":\\\"avatars\\\\/large_4157152-.jpg\\\",\\\"small\\\":\\\"avatars\\\\/small_4157152-41571521574718514.jpg\\\",\\\"bigger\\\":\\\"avatars\\\\/bigger_4157152-41571521574718514.jpg\\\",\\\"full\\\":\\\"avatars\\\\/full_4157152-41571521574718514.jpg\\\",\\\"profile\\\":\\\"avatars\\\\/profile_4157152-41571521574718514.jpg\\\",\\\"forum\\\":\\\"avatars\\\\/forum_4157152-41571521574718514.jpg\\\"}\",\"employee_avatar_url_small\":\"xxx\",\"schedule_id\":\"126235\",\"schedule_name\":\"Assistant\",\"schedule_color\":\"1\",\"timeclock_id\":\"154423384\",\"clockin_time\":{\"id\":4328,\"month\":3,\"day\":5,\"wday\":5,\"year\":2021,\"weekday\":\"Fri\",\"mname\":\"Mar\",\"week\":9,\"dayid\":6,\"timeid\":37,\"timeid_exact\":36,\"formatted\":\"Mar 5, 2021\",\"iso8601\":\"2021-03-05T08:56:40+0000\",\"time\":\"08:56\",\"timestamp\":1614934600,\"hours\":8,\"minutes\":56,\"seconds\":40,\"date\":\"Mar 5, 2021\",\"day_of_year\":\"63\",\"week_adjusted\":9,\"sp_wday\":6,\"date_timestamp\":\"2021-03-05 08:56:40\"},\"shift_id\":\"762556286\",\"shift_start\":{\"id\":4328,\"month\":3,\"day\":5,\"wday\":5,\"year\":2021,\"weekday\":\"Fri\",\"mname\":\"Mar\",\"week\":9,\"dayid\":6,\"timeid\":37,\"timeid_exact\":37,\"formatted\":\"Mar 5, 2021\",\"iso8601\":\"2021-03-05T09:00:00+0000\",\"time\":\"09:00\",\"timestamp\":1614934800,\"hours\":9,\"minutes\":0,\"seconds\":0,\"date\":\"Mar 5, 2021\",\"day_of_year\":\"63\",\"week_adjusted\":9,\"sp_wday\":6,\"date_timestamp\":\"2021-03-05 09:00:00\"},\"shift_end\":{\"id\":4328,\"month\":3,\"day\":5,\"wday\":5,\"year\":2021,\"weekday\":\"Fri\",\"mname\":\"Mar\",\"week\":9,\"dayid\":6,\"timeid\":51,\"timeid_exact\":51,\"formatted\":\"Mar 5, 2021\",\"iso8601\":\"2021-03-05T12:30:00+0000\",\"time\":\"12:30\",\"timestamp\":1614947400,\"hours\":12,\"minutes\":30,\"seconds\":0,\"date\":\"Mar 5, 2021\",\"day_of_year\":\"63\",\"week_adjusted\":9,\"sp_wday\":6,\"date_timestamp\":\"2021-03-05 12:30:00\"},\"is_on_break\":null,\"break\":[]}],\"metadata\":[],\"token\":\"xxx\",\"error\":null}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":200,"wires":[["cdd7229f.03f11"]]},{"id":"cdd7229f.03f11","type":"function","z":"66862370.3ad20c","name":"","func":"msg.payload.todd = msg.payload.data.find(e => e.employee_id == \"4157152\");\nreturn msg;","outputs":1,"noerr":0,"x":230,"y":200,"wires":[["583d7ae4.4f92e4"]]},{"id":"583d7ae4.4f92e4","type":"function","z":"66862370.3ad20c","name":"todd","func":"msg.payload = msg.payload.todd;\nmsg.payload.name = msg.payload.employee_name;\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":200,"wires":[["ad82d671.2406f8"]]},{"id":"ad82d671.2406f8","type":"function","z":"66862370.3ad20c","name":"","func":"const shiftstart = msg.payload.shift_start.timestamp;\nconst clockin = msg.payload.clockin_time.timestamp;\nconst timeDifference = shiftstart - clockin;\nconst timeDifference2 = clockin - shiftstart;\n    if ((timeDifference/60).toFixed(0) > 3){\n        msg.payload = msg.payload.name +\"  \" + (timeDifference/60).toFixed(0) + \"  early \";\n    } else if ((timeDifference/60).toFixed(0) < 3){\n        msg.payload = msg.payload.name +\"  \" + (timeDifference2/60).toFixed(0) + \"  late \";\n    }\n\nreturn msg;\n\n\n\n    \n//const shiftstart = msg.payload.shift_start.timestamp;\n//const clockin = msg.payload.clockin_time.timestamp;\n//const timeDifference = shiftstart - clockin;\n//msg.payload = \" \" + (timeDifference/60).toFixed(0) + \" early \";\n//return msg;    ","outputs":1,"noerr":0,"x":530,"y":200,"wires":[["302b798b.f93456"]]},{"id":"302b798b.f93456","type":"debug","z":"66862370.3ad20c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":690,"y":200,"wires":[]}]

many thanks if anyone has time to take a look! :slight_smile:

what are you getting vs what you are expecting?

PS, add debug nodes after every node to see what affect your code has on the payload (topi: name the debug nodes to simplify understanding)

(timeDifference/60).toFixed(0) > 3)

the toFixed makes it a string, then you compare it to a number, maybe this is the issue.
[edit] does it need to be rounded to compare if it is lower or higher than 3?
p.s. you also have no outcome if the difference is 3 exactly.

Thank you, that was it, timedifference having no else was the problem. Thank you!

Note that if you do round it then, for example, 3.4 will be converted to 3 and 3 is not >3. So you end up deciding that 3.4 is <= 3 which I would have thought is not what you want. Also as already pointed out comparing a string to a number will have unexpected results. The best practice is, almost always, don't reduce the precision until you are displaying the value.

[Edit] I had initially messed up the numbers above, now corrected.

1 Like

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