Get index of array

Hello again,
i get an array of objects from node-red-contib-arp (note: filtering for mac adresses is not possible/working in that node!):

image

now i try to get the array index of the searched mac adress (in this case 21) and output the index and the ip address.

In the future i dont know the ip adress and the index, only the mac adress is known.

i tried:

var index = msg.payload.indexOf('1c:87:76:b0:2a:68');
msg.payload = index;
return msg;

I also tied a change node:

image

But nothing is working as expected.
I am not familiar with change nodes / jsonata.

So at the moment for me it is a walk in the darkness.

Can someone help me?

Thanks to all in advance

If you want the full object for a specific mac you could use a function node...

var found = msg.payload.find(e => e.mac == '1c:87:76:b0:2a:68');
msg.payload = found;
return msg;

If you really want the index then you could loop through the array.

To find index there is .findIndex() method

var idx = msg.payload.findIndex(e => e.mac == '1c:87:76:b0:2a:68');
msg.payload = idx;
return msg;

If not found, -1 returned
If there is more than one, index of first occurrence returned.

That should be payload[mac='1c:87:76:b0:2a:68']
and should return a payload object containing ip mac and iface

1 Like

Thank you now it is working as expected!
:slightly_smiling_face: :+1:

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