# Search for same value in two arrays

**URL:** <https://discourse.nodered.org/t/search-for-same-value-in-two-arrays/63713>\
**Category:** General\
**Created:** [10 June 2022 10:03 UTC](https://discourse.nodered.org/t/search-for-same-value-in-two-arrays/63713 "2022-06-10T10:03:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![milner236](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@milner236](https://discourse.nodered.org/u/milner236)\
**Post date:** [10 June 2022 10:03 UTC](https://discourse.nodered.org/t/search-for-same-value-in-two-arrays/63713/1 "2022-06-10T10:03:57Z")

</div>

Hello all.  
I have two arrays that contain a dynamic number of objects (per input).  
 ![Screenshot_9](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/3077a41e2a970d61846b4d24d6a1b95c01556fcf.png)

I am trying to do the following:  
For each object in the first array, check if a certain value exists in any of the objects in the second array, and if so extract another value from that object.

I've tried doing so with the following function:

```auto
var array1 = msg.payload[0];
var array2 = msg.payload[1];
for(let i = 0; i < array2.length; i++){
    if (array2[i]["order_number"] === array1[i]["order_number"]){
        array2.push({"city" : array1[i]["city"]})
    }
}
msg.payload = array2;
return msg;

```

This works fine only if one of the arrays contain a single object. Any more than that and I receive an "order\_number" undefined.

I've tried looping through the entire second array within the first loop with the following:

```auto
var array2 = msg.payload[0];
var array1 = msg.payload[1];
for(let i = 0; i < array1.length; i++){
    for(let i = 0; i < array2.length; i++){
    if (array1[i]["order_number"] === array2[i]["order_number"]){
        array1.push({"city" : array2[i]["city"]})
    }
}}
msg.payload = array1;
return msg;

```

This however causes my node-red to crash each time I inject a payload.  
What am I doing wrong?

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![OriolFM](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oriolfm/32/30710_2.png) [@OriolFM](https://discourse.nodered.org/u/OriolFM)\
**Post date:** [10 June 2022 12:05 UTC](https://discourse.nodered.org/t/search-for-same-value-in-two-arrays/63713/2 "2022-06-10T12:05:20Z")

</div>

In the last example you added, you are using the same index for both arrays, and that will never work for what you're trying to do, because you're just comparing the objects with the same index in both arrays multiple times.

Try changing the index names:

```auto
var array2 = msg.payload[0];
var array1 = msg.payload[1];
for(let i = 0; i < array1.length; i++){
    for(let j = 0; j < array2.length; j++){
    if (array1[i]["order_number"] === array2[j]["order_number"]){
        array1.push({"city" : array2[j]["city"]})
    }
}}
msg.payload = array1;
return msg;

```

---

<div class="post-metadata">

**Author:** ![milner236](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@milner236](https://discourse.nodered.org/u/milner236)\
**Post date:** [10 June 2022 12:49 UTC](https://discourse.nodered.org/t/search-for-same-value-in-two-arrays/63713/3 "2022-06-10T12:49:21Z")

</div>

Thanks, I’ll check it out and post an update

---

<div class="post-metadata">

**Author:** ![milner236](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@milner236](https://discourse.nodered.org/u/milner236)\
**Post date:** [12 June 2022 04:04 UTC](https://discourse.nodered.org/t/search-for-same-value-in-two-arrays/63713/4 "2022-06-12T04:04:43Z")

</div>

As expected, your advice solved my issue. Much thanks.

---

<div class="post-metadata">

**Author:** ![OriolFM](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oriolfm/32/30710_2.png) [@OriolFM](https://discourse.nodered.org/u/OriolFM)\
**Post date:** [14 June 2022 09:44 UTC](https://discourse.nodered.org/t/search-for-same-value-in-two-arrays/63713/5 "2022-06-14T09:44:51Z")

</div>

You're welcome.

You should be extra careful with the index names in nested loops, and never repeat their names.

I usually go with i/j/k or x/y/z for different levels of nesting to avoid precisely this problem, but you can name them whatever you want as long as each identifier is unique (unless you work with diagonals in bidimensional arrays, but then you only need one level and one index to loop through).

---

<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 June 2022 09:44 UTC](https://discourse.nodered.org/t/search-for-same-value-in-two-arrays/63713/6 "2022-06-28T09:44:59Z")

</div>

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