# Remove end of array in payload

**URL:** <https://discourse.nodered.org/t/remove-end-of-array-in-payload/34511>\
**Category:** General\
**Created:** [19 October 2020 16:21 UTC](https://discourse.nodered.org/t/remove-end-of-array-in-payload/34511 "2020-10-19T16:21:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffreys](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@jeffreys](https://discourse.nodered.org/u/jeffreys)\
**Post date:** [19 October 2020 16:21 UTC](https://discourse.nodered.org/t/remove-end-of-array-in-payload/34511/1 "2020-10-19T16:21:19Z")

</div>

How do I remove 22 thru 27 of the data array as shown? I just want to pass out the first 21 data values.

To make the function 16 modbus write work i need to have the data array be the same as the quantity of values it writes. Im sure its an easy code line so i didnt bother putting the flow or values up properly but i can if anyone needs them.

i would think it would be something like msg.payload.value[21] = msg.payload.data[27]; but this isn't right

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35d01f9845ca99c20055c196e780d1b8b547d4a7.png)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [19 October 2020 16:25 UTC](https://discourse.nodered.org/t/remove-end-of-array-in-payload/34511/2 "2020-10-19T16:25:58Z")

</div>

You probably want to look at JS's .slice() function.

---

<div class="post-metadata">

**Author:** ![jeffreys](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@jeffreys](https://discourse.nodered.org/u/jeffreys)\
**Post date:** [19 October 2020 16:33 UTC](https://discourse.nodered.org/t/remove-end-of-array-in-payload/34511/3 "2020-10-19T16:33:39Z")

</div>

Looks like that would do it.

Trying  
msg.value = data(slice(0,21));

but doesn't work. Im sure just using the syntax wrong

---

<div class="post-metadata">

**Author:** ![jeffreys](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@jeffreys](https://discourse.nodered.org/u/jeffreys)\
**Post date:** [19 October 2020 16:47 UTC](https://discourse.nodered.org/t/remove-end-of-array-in-payload/34511/4 "2020-10-19T16:47:12Z")

</div>

This isn't pretty but works

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/55d8e343b0194e0e47c400fb3572556a746d6900.png)

---

<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:** [19 October 2020 16:56 UTC](https://discourse.nodered.org/t/remove-end-of-array-in-payload/34511/5 "2020-10-19T16:56:13Z")

</div>

> [@jeffreys](#):
>
> msg.value = data(slice(0,21));

```auto
msg.value = msg.payload.data.slice(0, 21)

```

[Array.prototype.slice()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice)

---

<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:** [19 October 2020 17:07 UTC](https://discourse.nodered.org/t/remove-end-of-array-in-payload/34511/6 "2020-10-19T17:07:28Z")

</div>

by the way .. i notice in your screenshot that you do a bunch of `delete` to clean up a msg.  
if its easier .. you could do the opposite and create a new msg with a new structure based on some of the values of the old msg.

```auto
let newMsg = {
"payload" : "new payload",
"values" : msg.payload.data.slice(0,21),
"topic" : "new topic"
}

return newMsg;

```

---

<div class="post-metadata">

**Author:** ![jeffreys](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@jeffreys](https://discourse.nodered.org/u/jeffreys)\
**Post date:** [19 October 2020 17:09 UTC](https://discourse.nodered.org/t/remove-end-of-array-in-payload/34511/7 "2020-10-19T17:09:08Z")

</div>

> [@UnborN](#):
>
> `msg.value = msg.payload.data.slice(0, 21)`

Thank you! That would be better. Drinking from the fire hose but getting it working

---

<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:** [19 October 2020 17:14 UTC](https://discourse.nodered.org/t/remove-end-of-array-in-payload/34511/8 "2020-10-19T17:14:07Z")

</div>

you are welcome but the solution was given by TotallyInformation 😉  
i just clarified a small part of how to use it correctly.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [19 October 2020 17:16 UTC](https://discourse.nodered.org/t/remove-end-of-array-in-payload/34511/9 "2020-10-19T17:16:24Z")

</div>

Best to use the Mozilla Developer Network to look up JS functions - I still have to do that all the time.

Also, deleting things from the msg can sometimes have odd effects due to the way that JavaScript passes variables by reference and not as a copy.

---

<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:** [2 November 2020 17:16 UTC](https://discourse.nodered.org/t/remove-end-of-array-in-payload/34511/10 "2020-11-02T17:16:32Z")

</div>

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