# How to loop an array?

**URL:** https://discourse.nodered.org/t/how-to-loop-an-array/20191
**Category:** General
**Created:** [11 January 2020 09:37 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191 "2020-01-11T09:37:39Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Golonder](https://avatars.discourse-cdn.com/v4/letter/g/eada6e/32.png) [@Golonder](https://discourse.nodered.org/u/Golonder)
#### Post date: [11 January 2020 09:37 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/1 "2020-01-11T09:37:39Z")

</div>

Hi,

can you please help me to find out how to go through an array and perform an action on every element in this array ?

Something like that:

```auto
MQTT_Array = ["sonoff/sonoff082/STATUS8","sonoff/sonoff084/SENSOR","sonoff/sonoff085/STATUS8","sonoff/sonoff086/STATUS8","sonoff/sonoff087/STATUS8","sonoff/sonoff089/SENSOR","sonoff/sonoff091/STATUS8","sonoff/sonoff092/SENSOR"]

for ("every element in MQTT_Array")
{
    var value = global.get('array_element.Value'); 
    if (value > 5) 
    {
        return msg;
    }
    else
    {
        return null;
    }
}

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 January 2020 10:53 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/2 "2020-01-11T10:53:44Z")

</div>

You can use every() on an array to invoke a function for every element. Alternatively if you put the array in msg.payload and pass it to a Split node you can split the array into a stream of messages which you can then pass to nodes to perform whatever action is desired.

---

<div class="post-metadata">

### Author: ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)
#### Post date: [11 January 2020 11:08 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/3 "2020-01-11T11:08:58Z")

</div>

Personally, I am using the array loop node from (node-red-contrib-loop-processing). I find it very convenient (and simple) for a beginner like me!  
GV

---

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [11 January 2020 12:06 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/4 "2020-01-11T12:06:02Z")

</div>

```auto
for (var element of array) {
node.warn(element)
}

```

or

```auto
for (var index in array) {
 node.warn(array[index])
}

```

Better (order consisted)

```auto
array.forEach(function (value,index) {
});

```

---

<div class="post-metadata">

### Author: ![iOnline247](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ionline247/32/15634_2.png) [@iOnline247](https://discourse.nodered.org/u/iOnline247)
#### Post date: [11 January 2020 15:51 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/5 "2020-01-11T15:51:11Z")

</div>

The `forEach` syntax is the only one that should be used in this instance. `for of` has its place, but is much slower than `forEach`. `for in` on an array should never be used.

---

<div class="post-metadata">

### Author: ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)
#### Post date: [11 January 2020 16:15 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/6 "2020-01-11T16:15:19Z")

</div>

> [@iOnline247](#):
>
> `for of` has its place, but is much slower than `forEach`

Interesting! Have you run performance tests on different access methods (and if so, can you describe how)?

Being a long-time C/C++ coder, I tend to use e.g.

> for (idx=0; idx\<array.length; idxx++)

which I'd assumed would be the fastest, but I'd be interested to hear if it actually was.

---

<div class="post-metadata">

### Author: ![iOnline247](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ionline247/32/15634_2.png) [@iOnline247](https://discourse.nodered.org/u/iOnline247)
#### Post date: [11 January 2020 16:18 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/7 "2020-01-11T16:18:26Z")

</div>

There's a bunch of tests on JSPerf. Check them out for the results. Your browser will impact the results, but it's a good measure nonetheless.

---

<div class="post-metadata">

### Author: ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)
#### Post date: [11 January 2020 16:23 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/8 "2020-01-11T16:23:54Z")

</div>

Thanks - I'll give it a go.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 January 2020 17:05 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/9 "2020-01-11T17:05:11Z")

</div>

> [@iOnline247](#):
>
> Your browser will impact the results,

Remember that it is nodejs that is executing the code not the browser, so strictly you would need to run the tests in node.js.

---

<div class="post-metadata">

### Author: ![iOnline247](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ionline247/32/15634_2.png) [@iOnline247](https://discourse.nodered.org/u/iOnline247)
#### Post date: [11 January 2020 17:14 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/10 "2020-01-11T17:14:01Z")

</div>

If you set the [tests up properly](https://stackoverflow.com/a/41115315), the difference is negligible. This makes sense since both environments are using the same engine, if you're using Chrome.

---

<div class="post-metadata">

### Author: ![Golonder](https://avatars.discourse-cdn.com/v4/letter/g/eada6e/32.png) [@Golonder](https://discourse.nodered.org/u/Golonder)
#### Post date: [11 January 2020 19:10 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/11 "2020-01-11T19:10:42Z")

</div>

to be honest, I do not really get the "function (value,index)" part of this solution ... what can I do with that ?

```auto
MQTT_Array = ["sonoff/sonoff082/STATUS8","sonoff/sonoff084/SENSOR","sonoff/sonoff085/STATUS8","sonoff/sonoff086/STATUS8","sonoff/sonoff087/STATUS8","sonoff/sonoff089/SENSOR","sonoff/sonoff091/STATUS8","sonoff/sonoff092/SENSOR"]

MQTT_Array.forEach(function (value,index) {
});

for ("every element in MQTT_Array")
{
    var value = global.get('array_element.Value'); 
    if (value > 5) 
    {
        return msg;
    }
    else
    {
        return null;
    }
}

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 January 2020 19:41 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/12 "2020-01-11T19:41:15Z")

</div>

```auto
MQTT_Array.forEach(function (value,index) {
   if (value > 5) 
    {
        return msg;
    }
    else
    {
        return null;
    }
});

```

However I am not sure that is actually what you want to do. What exactly are you trying to achieve?

---

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [11 January 2020 21:48 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/13 "2020-01-11T21:48:49Z")

</div>

What should be used for nested loops. Many editors complain not to define a function inside an forEach function. Is the warning related to the stack or performance?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 January 2020 21:55 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/14 "2020-01-11T21:55:28Z")

</div>

> [@Christian-Me](#):
>
> Many editors complain not to define a function inside an forEach function

I probably would not do that, mostly because keeping track of the brackets and parentheses becomes a nightmare. The cleaner way to do it is to take the inline function definition out. So you end up with something like

```auto
MQTT_Array.forEach(doIt);

function doit(value,index) {
   if (value > 5) 
    {
        return msg;
    }
    else
    {
        return null;
    }
}

```

And do the same thing again if there is a nested `forEach()` required.

---

<div class="post-metadata">

### Author: ![iOnline247](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ionline247/32/15634_2.png) [@iOnline247](https://discourse.nodered.org/u/iOnline247)
#### Post date: [14 January 2020 02:24 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/15 "2020-01-14T02:24:52Z")

</div>

```javascript
function doStuff(/*params that make sense */) {

}

arr.map(val => {
   returm doStuff(val);
});

```

Alternatively, you can write these a bit more terse.

```javascript
function convertToObjects (value) {
    return mapValToProps(value);
}

function mapValToProps(value) {
    return {
        lightBulb: value.ah_Ha;
    };
}

arr.map(convertToObjects);

```

Most of the examples you see have inlined functions, but that doesn't mean you can define the function elsewhere and use it as a callback. 😉 When I'm debugging, sometimes I'll even do this `arr.forEach(console.log);`

---

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [14 January 2020 07:41 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/16 "2020-01-14T07:41:58Z")

</div>

Thank you for sharing another approach. Perhaps it is important to point out that there is a different usecase for forEach and map

As I read the docs the map method is useful if you need each result of each iteration as map returns a new array containing the results  
of each iteration

> The `map()` method creates a new array with the results of calling a function for every array element.
> 
> The `map()` method calls the provided function once for each element in an array, in order.

In your first example I would use forEach() instead of map() because the result will be unused.

BTW: console.log by itself does nice colorful formatting of arrays and objects by itself.

> [@Debugging ... little tweaks that perhaps can be useful](https://discourse.nodered.org/t/debugging-little-tweaks-that-perhaps-can-be-useful/19542/9):
>
> Sorry ... not working (here) node.warn('Foo speaking: ${config}'); [image] And this as expected: node.warn('Foo speaking: '+config); [image] and what I normaly do, nice object view but two messages: node.warn('Foo speaking:'); node.warn(config); [image] this works but that's not the way to do it, right? console.log('Foo speaking:',config); [image]

There is a rundown of many possible debug log options including console.log()

---

<div class="post-metadata">

### Author: ![jokovkilg](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@jokovkilg](https://discourse.nodered.org/u/jokovkilg)
#### Post date: [24 November 2021 06:26 UTC](https://discourse.nodered.org/t/how-to-loop-an-array/20191/17 "2021-11-24T06:26:39Z")

</div>

for in loops over enumerable property names of an object.

for of (new in ES6) does use an object-specific iterator and loops over the values generated by that.

"[forEach](http://net-informations.com/q/js/foreach.html)...in" iterates a specified variable over all values of the specified object's properties.
