# Use variable as an index to udpate flow array object

**URL:** <https://discourse.nodered.org/t/use-variable-as-an-index-to-udpate-flow-array-object/41750>\
**Category:** General\
**Created:** [28 February 2021 09:47 UTC](https://discourse.nodered.org/t/use-variable-as-an-index-to-udpate-flow-array-object/41750 "2021-02-28T09:47:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![notnice](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@notnice](https://discourse.nodered.org/u/notnice)\
**Post date:** [28 February 2021 09:47 UTC](https://discourse.nodered.org/t/use-variable-as-an-index-to-udpate-flow-array-object/41750/1 "2021-02-28T09:47:47Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/e/ae32b8238ffff8879027b53931a7067bf8d8a0f5.png)

I have a flow array "apgroupSelection" and declare that in the 1st function (left) node context using:

```auto
apgroupSelection = flow.get("apgroupSelection");

```

I go through a loop and push data into the array using:

```auto
pgroupSelection.push({id: flow.get("apgroupName"),groupId: flow.get("apgroupID"),threshold: flow.get("threshold"),alert: false});

```

Each time I push the data in the 1st function node I also send an HTTP request and then do some processing in the 2nd function node. From the 1st function node I also send `msg.loopCounter = <loopCounter value>` to the 2nd function node so it knows which array index is currently being processed.

In the 2nd function node I want to update the value of "alert:" directly in the flow array using the loopCounter as the array index, rather than having to declare the flow array in context each time this function node is called. Tried the parameters below:

```auto
flow.set("apgroupSelection[msg.loopCounter].alert", true);

```

Also tried declaring loopCounter as a variable and just using [loopCounter], and tried [+loopCounter] without success. It seems to expect only a numeric index number in `[]` not a variable?

Anyway to achieve what I'm trying to do without having to declare the array in the 2nd fucntion node each time I call this function?

---

<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:** [28 February 2021 10:58 UTC](https://discourse.nodered.org/t/use-variable-as-an-index-to-udpate-flow-array-object/41750/2 "2021-02-28T10:58:21Z")

</div>

`flow.set("test["+msg.payload+"].count",msg.payload)`  
just tested and the above works for me. payload is a number.

---

<div class="post-metadata">

**Author:** ![notnice](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@notnice](https://discourse.nodered.org/u/notnice)\
**Post date:** [28 February 2021 11:10 UTC](https://discourse.nodered.org/t/use-variable-as-an-index-to-udpate-flow-array-object/41750/3 "2021-02-28T11:10:36Z")

</div>

Hello and thx for your super-quick repsonse. I just used a variation of your answer to fit my parameters below and it now work great 👍

```auto
flow.set("apgroupSelection[" + loopCounter + "].alert", true);

```

---

<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:** [14 March 2021 11:11 UTC](https://discourse.nodered.org/t/use-variable-as-an-index-to-udpate-flow-array-object/41750/4 "2021-03-14T11:11:14Z")

</div>

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