# Working with an Array in a SubFlow

**URL:** https://discourse.nodered.org/t/working-with-an-array-in-a-subflow/46383
**Category:** General
**Created:** [28 May 2021 05:50 UTC](https://discourse.nodered.org/t/working-with-an-array-in-a-subflow/46383 "2021-05-28T05:50:08Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![akd02](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@akd02](https://discourse.nodered.org/u/akd02)
#### Post date: [28 May 2021 05:50 UTC](https://discourse.nodered.org/t/working-with-an-array-in-a-subflow/46383/1 "2021-05-28T05:50:08Z")

</div>

**Hello Community,**

**I need help on this one. I want to create a subflow that maintains a “list” in the form of an array of objects.**

**I would like to let the user configure a “uniqueID” being a property such that the list will not have entries with dupliacteIDs.**

**My problem is in the “findEntryOnList” function mentioned below where I would like the “item.details.credentialNumber” become a variable based on the subflow’s Environment variable… and I am not able to do that.**

**Any suggestions how to fix this?**

**Below is a snap of my code in the function inside the subFlow:**

//This function maintains a list of unique entries in an array  
//This function will compare the incoming message's "uniqueID" property with the mainatined list  
// if the "uniqueID" exists on the list -\> the messages replaces the existing entry on the list  
// if the "uniqueID" does not exist -\> the message is added to the list

var list = context.get("list") || ; // load the list from the node's memory

//UniqueID  
var prop = env.get("UniqueID");  
var value = RED.util.getMessageProperty(msg,prop);  
var uniqueId = value;  
 ![SubFlow Variable is a Property Name](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/f/af45b556810092db315d4c15dc70056546f33a53.png)

function findEntryOnList (item){ return item.details.credentialNumber == uniqueId; } // This works!!!!

//function findEntryOnList (item){ return **item[prop]** == uniqueId; } // This Doesn't work ☹

var index1 = list.findIndex(findEntryOnList);

---

<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: [28 May 2021 11:41 UTC](https://discourse.nodered.org/t/working-with-an-array-in-a-subflow/46383/2 "2021-05-28T11:41:26Z")

</div>

> [@akd02](#):
>
> //function findEntryOnList (item){ return **item[prop]** == uniqueId; } // This Doesn't work ☹

square bracket notation doesnt do sub property evaluation.

e.g. if prop = "payload.details.credentialNumber" then item[prop] is looking for item["payload.details.credentialNumber"];

in JSON it becomes more obvious

```auto
{
  item : { 
    "payload.details.credentialNumber" : "value" 
  }
}

```

  

try this (untested)...

```auto
var found = list.find( item => RED.util.getMessageProperty(item, prop) != null )
if(found) {
  ...
}

```

---

<div class="post-metadata">

### Author: ![akd02](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@akd02](https://discourse.nodered.org/u/akd02)
#### Post date: [28 May 2021 12:02 UTC](https://discourse.nodered.org/t/working-with-an-array-in-a-subflow/46383/3 "2021-05-28T12:02:35Z")

</div>

Thanks for the suggestion Steve.

i am not that good in JavaScripts & the 'item =\>' method 🙂

I've tried your suggestion but i get: ""TypeError: Cannot read property 'details' of undefined"

Not sure if this can help... if it does it would be truly appreciated!... below is the entire content of my function "in the sSbFlow":

```auto
var list = context.get("list") || []; // load the list from the node's memory

if (msg.payload =="") // send out the list content when empty payload is rx
    {
    msg.payload = list;
    return msg;
    }

//UniqueID
var prop = env.get("UniqueID");
var uniqueId = RED.util.getMessageProperty(msg,prop); // the "credentialNumber" or card ID is what we want to be unique in our list
var found = list.find( item => RED.util.getMessageProperty(item, prop) != null )

if (msg.listControl == "add")
    {
        //if message with the same "credentialNumber" is not already the new entry to the list, then add it to the list
        if (!found) {
            list.push(msg.payload); //the push() method adds new items to the end of an array
        }  
        
        // if message with the same "credentialNumber" is already in the list, then update the old entry with a new one
         if (found) {
            list.splice(index1,1); // remove old entry
            list.push(msg.payload); // add new entry
        }
    }

if (msg.listControl == "remove")
    {
        //if message with the same "credentialNumber" is not already the new entry to the list, then add it to the list
        if (!found) {
             //do nothing
        }  
        
         // if message with the same "credentialNumber" is already in the list, then remove entry from list
         if (found) {
            list.splice(index1,1); // remove entry
        }
    }
    
if (msg.listControl == "reset")
    {
       list=[];
    }
    

context.set("list", list); // save the list in node's memory
msg.payload = list;

return msg;

```

---

<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: [28 May 2021 12:06 UTC](https://discourse.nodered.org/t/working-with-an-array-in-a-subflow/46383/4 "2021-05-28T12:06:41Z")

</div>

> [@akd02](#):
>
> I've tried your suggestion but i get: ""TypeError: Cannot read property 'details' of undefined"

show me what is in the context  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/8/b8e302c8c61806bbceb28c8f89248f4b10672321.png)

also, add `node.warn(prop)` below `var prop = env.get("UniqueID");` and show me what `prop` contains.

My bet is the items in context do not have said `prop`

---

<div class="post-metadata">

### Author: ![akd02](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@akd02](https://discourse.nodered.org/u/akd02)
#### Post date: [28 May 2021 12:14 UTC](https://discourse.nodered.org/t/working-with-an-array-in-a-subflow/46383/5 "2021-05-28T12:14:35Z")

</div>

Hi Steve,

my context bar is in fact empty "selected the subflow node on the flow, even the function node inside the subflow, did the refresh and still it was empty in both cases"

as for the output of the node.warn(prop) command, i get the following:

"  
function : (warn)  
"payload.details.credentialNumber"  
"

---

<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: [28 May 2021 12:16 UTC](https://discourse.nodered.org/t/working-with-an-array-in-a-subflow/46383/6 "2021-05-28T12:16:38Z")

</div>

ok, change the node.warn to `node.warn([list,prop])` - show me what you see in debug window when you operate it (expand all properties)

---

<div class="post-metadata">

### Author: ![akd02](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@akd02](https://discourse.nodered.org/u/akd02)
#### Post date: [28 May 2021 12:18 UTC](https://discourse.nodered.org/t/working-with-an-array-in-a-subflow/46383/7 "2021-05-28T12:18:28Z")

</div>

here's what I get:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/3482483288f57c55eb1672a93368d5e60f3ef0db.png)

---

<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: [28 May 2021 12:20 UTC](https://discourse.nodered.org/t/working-with-an-array-in-a-subflow/46383/8 "2021-05-28T12:20:30Z")

</div>

So you are asking the `find` function to find `payload.details.credentialNumber` in object `item`

As you can see, the object/item has no `payload` property.

if you use `details.credentialNumber` as the UniqueId it should work

---

<div class="post-metadata">

### Author: ![akd02](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@akd02](https://discourse.nodered.org/u/akd02)
#### Post date: [28 May 2021 12:26 UTC](https://discourse.nodered.org/t/working-with-an-array-in-a-subflow/46383/9 "2021-05-28T12:26:39Z")

</div>

Thanks but that would break the logic of the following code, wouldn't it????

var prop = env.get("UniqueID");  
var uniqueId = RED.util.getMessageProperty(msg,prop); // the "credentialNumber" or card ID is what we want to be unique in our list

P.S. I tried using "details.credentialNumber" as my SubFlow's environmental Variable, i still gt the same error:

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

---

<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: [28 May 2021 12:29 UTC](https://discourse.nodered.org/t/working-with-an-array-in-a-subflow/46383/10 "2021-05-28T12:29:58Z")

</div>

> [@akd02](#):
>
> Thanks but that would break the logic of the following code, wouldn't it????

Yes, but that is because what you search is different to what you store. e,g, you search `msg` but store `msg.payload` in your list (meaning the list objects do not have a payload property)

There are 2 solutions. the easiest / best is ...

Change the UniqueId to `details.credentialNumber` and change the 2nd line to `var uniqueId = RED.util.getMessageProperty(msg.payload, prop);`

---

<div class="post-metadata">

### Author: ![akd02](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@akd02](https://discourse.nodered.org/u/akd02)
#### Post date: [29 May 2021 08:05 UTC](https://discourse.nodered.org/t/working-with-an-array-in-a-subflow/46383/11 "2021-05-29T08:05:46Z")

</div>

Hi Steve,

i would still need to find the index of the entry matching the UniqueID on my list "to give a value to my index1 variable" otherwise my splice & push functions won't work "that's sadly what is happening now".

any suggestions?

---

<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: [29 May 2021 08:40 UTC](https://discourse.nodered.org/t/working-with-an-array-in-a-subflow/46383/12 "2021-05-29T08:40:49Z")

</div>

This is why I usually use an object instead of an array. No searching, no splicing etc.

E.g...

```auto
var list = context.get("list") || {}; // load the list object 

var prop = aUniqueKeyToItem;

//Find item
var foundItem = list[prop]

//use item
if (foundItem) {
  msg.payload = foundItem;
  return msg;
}

//Remove item
if (foundItem) {
  delete list[prop]
}

//Add item
if (!foundItem) {
  list[prop] = msg.payload;
}

```

This is not a complete solution, just some hints. Search the net for using an object as a lookup. It is much faster and simpler than using an array.

If you insist on sticking with an array, then you can use [findIndex](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex)

---

<div class="post-metadata">

### Author: ![akd02](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@akd02](https://discourse.nodered.org/u/akd02)
#### Post date: [29 May 2021 09:12 UTC](https://discourse.nodered.org/t/working-with-an-array-in-a-subflow/46383/13 "2021-05-29T09:12:14Z")

</div>

Thanks Steve, i will give your suggestion a try.... the reason why i went with an array of objects because each message i receive is a nested object by itself, hence i went with arrays to make use of core nodes like split & join... but perhaps your suggestion could be simpler.

furthermore, the findIndex method is where i problem started!

Remember my initial code had:

function findEntryOnList (item){ return item. **details.credentialNumber** == uniqueId; } // This works!!!!  
//function findEntryOnList (item){ return **item[prop]** == uniqueId; } // This Doesn't work ☹  
var index1 = list.findIndex(findEntryOnList);

the "item.details.credentialNumber" was the bit troubling me and wanted it to become a variable... if you still have suggestions on a quick fix for this one i would appreciate it.

---

<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: [28 July 2021 09:12 UTC](https://discourse.nodered.org/t/working-with-an-array-in-a-subflow/46383/14 "2021-07-28T09:12:48Z")

</div>

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