# Modifying every element of an array

**URL:** <https://discourse.nodered.org/t/modifying-every-element-of-an-array/45080>\
**Category:** General\
**Created:** [3 May 2021 01:53 UTC](https://discourse.nodered.org/t/modifying-every-element-of-an-array/45080 "2021-05-03T01:53:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajaymccollum](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@ajaymccollum](https://discourse.nodered.org/u/ajaymccollum)\
**Post date:** [3 May 2021 01:53 UTC](https://discourse.nodered.org/t/modifying-every-element-of-an-array/45080/1 "2021-05-03T01:53:32Z")

</div>

I know I keep asking javascript questions here, but every step you guys help me overcome reveals another problem to fix. I promise I gave it my all on this one, but I can't seem to find a way out.

I'd like to modify every element of an array the same way. So make [1,2,3,4] into [n1,n2,n3,n4]. Now the forEach() is able to do this fairly easily, but I think my problem may be more specific. What I'm trying to do is make every item in an array into an object. So [1,2,3] becomes [{"item": 1},{"item": 2},{"item": 3}].  
Man, .forEach() wants to do it so bad, but I'm getting that stringified "[object Object]" thing in the debug node, and I just don't know how to parse it out. Node.send() will spit out each item individually, so I thought I might just build another array with .push()...but I'm making things goofy at that point.

Thanks in advance for the great help!

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [3 May 2021 02:31 UTC](https://discourse.nodered.org/t/modifying-every-element-of-an-array/45080/2 "2021-05-03T02:31:13Z")

</div>

Hi .. forEach() and push() would be my approach also  
i dunno exactly what you have tried but here's an example

```auto
let newArray = []
msg.payload.forEach( el => newArray.push({item: el}) )
msg.payload = newArray;
return msg;

```

Flow:

```auto
[{"id":"f7c2e7cb.b1fdc","type":"inject","z":"8889c53d.0c1208","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[1,2,3,4]","payloadType":"json","x":330,"y":380,"wires":[["68b77baa.92d79c"]]},{"id":"68b77baa.92d79c","type":"function","z":"8889c53d.0c1208","name":"","func":"let newArray = []\n\nmsg.payload.forEach( el => newArray.push({item: el}) )\nmsg.payload = newArray;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":380,"wires":[["3abded27.50e27a"]]},{"id":"3abded27.50e27a","type":"debug","z":"8889c53d.0c1208","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":720,"y":380,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![ajaymccollum](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@ajaymccollum](https://discourse.nodered.org/u/ajaymccollum)\
**Post date:** [3 May 2021 03:04 UTC](https://discourse.nodered.org/t/modifying-every-element-of-an-array/45080/3 "2021-05-03T03:04:30Z")

</div>

Thank you very much. This works great. I actually didn't even try finishing out my way with the push(), because I figured I was making extra steps. Yours is more elegant than what I was about to do, so I'm glad I asked anyways. I gotta learn those arrow functions or whatever they're called. A lot of basic things still way beyond me just yet.  
Thanks again!

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [3 May 2021 03:12 UTC](https://discourse.nodered.org/t/modifying-every-element-of-an-array/45080/4 "2021-05-03T03:12:11Z")

</div>

There is a very good tutorial on youtube by Brad Traversy that covers some examples of  
`forEach(), map(), sort(), filter()`

the video covers both the use of normal functions and arrow function.  
definitely worth watching

**[JavaScript Higher Order Functions & Arrays](https://www.youtube.com/watch?v=rRgD1yVwIvE)**

---

<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:** [17 May 2021 03:12 UTC](https://discourse.nodered.org/t/modifying-every-element-of-an-array/45080/5 "2021-05-17T03:12:14Z")

</div>

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