# Get index of array

**URL:** <https://discourse.nodered.org/t/get-index-of-array/46809>\
**Category:** General\
**Created:** [7 June 2021 12:33 UTC](https://discourse.nodered.org/t/get-index-of-array/46809 "2021-06-07T12:33:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MAHRULEZ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mahrulez/32/41276_2.png) [@MAHRULEZ](https://discourse.nodered.org/u/MAHRULEZ)\
**Post date:** [7 June 2021 12:33 UTC](https://discourse.nodered.org/t/get-index-of-array/46809/1 "2021-06-07T12:33:29Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/6/6664fe0c4c38caaf3f42c7985ad98e77fb37fd7a.png)

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:

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

```

I also tied a change node:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/2/d2b1bb4637082ff0f2ba6ec9fa3636e4ce385341.png)

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [7 June 2021 12:53 UTC](https://discourse.nodered.org/t/get-index-of-array/46809/2 "2021-06-07T12:53:41Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [7 June 2021 13:12 UTC](https://discourse.nodered.org/t/get-index-of-array/46809/3 "2021-06-07T13:12:37Z")

</div>

To find index there is` .findIndex()` method

> **[Array.prototype.findIndex() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex)**
>
> The findIndex() method returns
> the index of the first element in the array that satisfies
> the provided testing function. Otherwise, it returns -1,
> indicating that no element passed the test.

```auto
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.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [7 June 2021 13:44 UTC](https://discourse.nodered.org/t/get-index-of-array/46809/4 "2021-06-07T13:44:37Z")

</div>

> [@MAHRULEZ](#):
>
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/2/d2b1bb4637082ff0f2ba6ec9fa3636e4ce385341.png)

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

---

<div class="post-metadata">

**Author:** ![MAHRULEZ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mahrulez/32/41276_2.png) [@MAHRULEZ](https://discourse.nodered.org/u/MAHRULEZ)\
**Post date:** [7 June 2021 14:04 UTC](https://discourse.nodered.org/t/get-index-of-array/46809/5 "2021-06-07T14:04:54Z")

</div>

Thank you now it is working as expected!  
🙂 👍

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [21 June 2021 14:05 UTC](https://discourse.nodered.org/t/get-index-of-array/46809/6 "2021-06-21T14:05:02Z")

</div>

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