# How to extract array index number?

**URL:** <https://discourse.nodered.org/t/how-to-extract-array-index-number/77773>\
**Category:** General\
**Created:** [21 April 2023 04:55 UTC](https://discourse.nodered.org/t/how-to-extract-array-index-number/77773 "2023-04-21T04:55:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jenssen](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@jenssen](https://discourse.nodered.org/u/jenssen)\
**Post date:** [21 April 2023 04:55 UTC](https://discourse.nodered.org/t/how-to-extract-array-index-number/77773/1 "2023-04-21T04:55:43Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [21 April 2023 05:43 UTC](https://discourse.nodered.org/t/how-to-extract-array-index-number/77773/2 "2023-04-21T05:43:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jenssen](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@jenssen](https://discourse.nodered.org/u/jenssen)\
**Post date:** [21 April 2023 06:05 UTC](https://discourse.nodered.org/t/how-to-extract-array-index-number/77773/3 "2023-04-21T06:05:56Z")

</div>

Hello,

Only this -\> So are you saying you are just interested in finding the total number of objects in the array that is 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:** [21 April 2023 06:45 UTC](https://discourse.nodered.org/t/how-to-extract-array-index-number/77773/4 "2023-04-21T06:45:28Z")

</div>

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

```auto
msg.payload = msg.payload.data.length;
return msg;

```

---

<div class="post-metadata">

**Author:** ![jenssen](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@jenssen](https://discourse.nodered.org/u/jenssen)\
**Post date:** [21 April 2023 15:48 UTC](https://discourse.nodered.org/t/how-to-extract-array-index-number/77773/6 "2023-04-21T15:48:34Z")

</div>

Perfect, works, many thanks.

Thread can be closed.

---

<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:** [5 May 2023 15:49 UTC](https://discourse.nodered.org/t/how-to-extract-array-index-number/77773/7 "2023-05-05T15:49:01Z")

</div>

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