How to extract array index number?

Hello,

I make an API call, the result is msg.payload : Object, which contains an array with data. I am not interested in the data itself, only in the indexnumber of the array.

For example the result is the output below, I want to extract the number 4.
msg.payload : Object
{ data: array[4] }

The results differ, so one time it is { data: array[4] }, or { data: array[4=8] } (then I want to extract the 8), or { data: array[2] }, then I want to extract the 2.

Is there a simple way to do this?

Thanks,

So are you saying you are just interested in finding the total number of objects in the array that is returned - or are you looking for a specific payload/value and then want to know the index number for that ?

Craig

Hello,

Only this -> So are you saying you are just interested in finding the total number of objects in the array that is returned.

To get the length of an array, in a change node
set msg. payload
to JSONata J: $count($$.payload.data)
where payload.data is the path to the array

Or in a function node

msg.payload = msg.payload.data.length;
return msg;
1 Like

Perfect, works, many thanks.

Thread can be closed.

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