# Modify an array

**URL:** <https://discourse.nodered.org/t/modify-an-array/17533>\
**Category:** General\
**Created:** [5 November 2019 10:29 UTC](https://discourse.nodered.org/t/modify-an-array/17533 "2019-11-05T10:29:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dave/32/10827_2.png) [@Dave](https://discourse.nodered.org/u/Dave)\
**Post date:** [5 November 2019 10:29 UTC](https://discourse.nodered.org/t/modify-an-array/17533/1 "2019-11-05T10:29:38Z")

</div>

I need a bit of help, I have the following, I want check how many arrays there are, in this case [4] delete the last one ie number 3 , then I want to add 2 more arrays to the bottom, so next time around there will be [5] arrays, delete the last one ie number 4 , then I want to add 2 more arrays to the bottom, and so on. I spent most of the afternoon yesterday trying to achieve this but without success, help would be appreciated thanks.

msg.payload : Object  
object  
name: "current\_temp"  
points: array[4]  
0: object  
x: "00:00"  
y: 18  
1: object  
x: "03:00"  
y: 18  
2: object  
x: "17:02"  
y: 21.5  
3: object  
x: "23:59"  
y: 18

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [5 November 2019 11:16 UTC](https://discourse.nodered.org/t/modify-an-array/17533/2 "2019-11-05T11:16:57Z")

</div>

> I want to add 2 more arrays

Your payload is 1 array with 4 objects.

You can use a function node to manipulate the elements in the array:

```auto
array = [{x: "00:00",y: 18},{x: "03:00",y: 18},{x: "17:02",y: 21.5},{x: "23:59",y: 18}]
node.send({text:"full array",payload:array})

// remove last element from array
array.pop()
node.send({text:"last removed",payload:array})

// add element to the end of the array
array.push({x: "14:02",y: 16})
node.send({text:"new element added",payload:array})

// add another element to the end of the array
array.push({x: "16:30",y: 16})
node.send({text:"another element added",payload:array})

```

example flow

```auto
[{"id":"495583a5.7458dc","type":"inject","z":"1971ca29.c484de","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":468,"y":240,"wires":[["39988e91.989fd2"]]},{"id":"39988e91.989fd2","type":"function","z":"1971ca29.c484de","name":"","func":"array = [{x: \"00:00\",y: 18},{x: \"03:00\",y: 18},{x: \"17:02\",y: 21.5},{x: \"23:59\",y: 18}]\nnode.send({text:\"full array\",payload:array})\n\n\n// remove last element from array\narray.pop()\nnode.send({text:\"last removed\",payload:array})\n\n// add element to the end of the array\n\narray.push({x: \"14:02\",y: 16})\nnode.send({text:\"new element added\",payload:array})\n\n// add another element to the end of the array\narray.push({x: \"16:30\",y: 16})\nnode.send({text:\"another element added\",payload:array})","outputs":1,"noerr":0,"x":602,"y":240,"wires":[["378802c0.36f926"]]},{"id":"378802c0.36f926","type":"debug","z":"1971ca29.c484de","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":722,"y":240,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [5 November 2019 12:43 UTC](https://discourse.nodered.org/t/modify-an-array/17533/3 "2019-11-05T12:43:10Z")

</div>

This page give an intro to javascript arrays and lists all the array modifiers  
You can then use a function node to achieve what you want

> **[Javascript Array Example | Arrays in Javascript](https://appdividend.com/2019/06/11/javascript-array-tutorial-with-example-arrays-in-javascript/)**
>
> Javascript Array Example | Arrays in Javascript. We will see how to create array, add elements in the array, display an array and methods.

---

<div class="post-metadata">

**Author:** ![Dave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dave/32/10827_2.png) [@Dave](https://discourse.nodered.org/u/Dave)\
**Post date:** [5 November 2019 13:39 UTC](https://discourse.nodered.org/t/modify-an-array/17533/4 "2019-11-05T13:39:00Z")

</div>

Thanks @bakman2 after your flow inspired me, I finally have it working, I had been trying **JavaScript array** manipulation but could not get past stuff like: 'pop() is not a function' errors anyway its working great now thanks
