# Loop array values as msg path parts

**URL:** https://discourse.nodered.org/t/loop-array-values-as-msg-path-parts/30939
**Category:** General
**Created:** [30 July 2020 16:27 UTC](https://discourse.nodered.org/t/loop-array-values-as-msg-path-parts/30939 "2020-07-30T16:27:27Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![rjj175](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@rjj175](https://discourse.nodered.org/u/rjj175)
#### Post date: [30 July 2020 16:27 UTC](https://discourse.nodered.org/t/loop-array-values-as-msg-path-parts/30939/1 "2020-07-30T16:27:27Z")

</div>

Novice here and going crazy. I'm trying to loop through the array values below to pull object elements out of the message and use them for additional calculations. I've tried every syntax I can think of and this is the closest I've come. However, I am getting the error shown in the debug snippet below on the first value. You can also see in the snippet that the ...atrisk value is in the message. Any assistance here is greatly appreciated.

Function node script...

var myArray = ["SPAQ", "NKLA", "SHLL", "ARLO", "TSLA", "FTNT", "DXCM", "DNOW"];

function netnow(i){  
var atrisk = msg.holding.myArray[i].atrisk;  
atrisk = atrisk.slice(1);  
atrisk = atrisk.replace(/,/g,'');  
atrisk = Number(atrisk);  
//more calculations will go here  
msg.holding.myArray[i].sellnow = atrisk;  
}

var i;  
for (i = 0; i \< myArray.length; i++){  
netnow(myArray[i]);  
}

return msg;

Debug output = "TypeError: Cannot read property 'SPAQ' of undefined"

 ![debug](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/a/baef1f86508e9da2be4f8d62e61dea245cb7e6b9.png)

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [30 July 2020 16:44 UTC](https://discourse.nodered.org/t/loop-array-values-as-msg-path-parts/30939/2 "2020-07-30T16:44:37Z")

</div>

Hi and welcome to the forum!

Please format code snippets properly using the `</>` icon on the editor. It works best if you first click it and then paste the code in the marked area between the backtick lines.

I can see at least two bugs in your code. Line:

```auto
var atrisk = msg.holding.myArray[i].atrisk;

```

... should be:

```auto
var atrisk = msg.holding[i].atrisk;

```

And similarly line:

```auto
msg.holding.myArray[i].sellnow = atrisk;

```

... should be:

```auto
msg.holding[i].sellnow = atrisk;

```

Edit:

This line looks useless also as the value does not contain commas.

```auto
atrisk = atrisk.replace(/,/g,'');

```

---

<div class="post-metadata">

### Author: ![rjj175](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@rjj175](https://discourse.nodered.org/u/rjj175)
#### Post date: [30 July 2020 16:48 UTC](https://discourse.nodered.org/t/loop-array-values-as-msg-path-parts/30939/3 "2020-07-30T16:48:07Z")

</div>

I can't thank you enough!!!  
That is a syntax I frankly don't think I ever would have tried. 😀

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [30 July 2020 17:03 UTC](https://discourse.nodered.org/t/loop-array-values-as-msg-path-parts/30939/4 "2020-07-30T17:03:12Z")

</div>

I believe you could also simplify your code a bit:

```auto
Object.keys(msg.holding).forEach(key => {
  let atrisk = msg.holding[key].atrisk;
  atrisk = atrisk.slice(1);
  atrisk = Number(atrisk);
  // more calculations will go here
  msg.holding[key].sellnow = atrisk;
});

return msg;

```

I'm assuming here you want to process all the objects under `msg.holding`. I can't see from your screenshot if there's more stock symbols there and you want to specifically handle only the listed ones.

---

<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: [30 July 2020 17:06 UTC](https://discourse.nodered.org/t/loop-array-values-as-msg-path-parts/30939/5 "2020-07-30T17:06:45Z")

</div>

syntax error, you forgot to close the foreach(

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [30 July 2020 18:09 UTC](https://discourse.nodered.org/t/loop-array-values-as-msg-path-parts/30939/6 "2020-07-30T18:09:22Z")

</div>

Oops and thanks! Fixed.

---

<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 September 2020 18:09 UTC](https://discourse.nodered.org/t/loop-array-values-as-msg-path-parts/30939/7 "2020-09-28T18:09:22Z")

</div>

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