# Removing items from an array

**URL:** <https://discourse.nodered.org/t/removing-items-from-an-array/44716>\
**Category:** General\
**Created:** [25 April 2021 21:45 UTC](https://discourse.nodered.org/t/removing-items-from-an-array/44716 "2021-04-25T21:45:35Z")\
**Posts on this page:** 6\
**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:** [25 April 2021 21:45 UTC](https://discourse.nodered.org/t/removing-items-from-an-array/44716/1 "2021-04-25T21:45:36Z")

</div>

Hey all,  
I'm hoping to hit an existing array with another array to remove the specific items from the original.  
Like if I have [22, 33, 44, 55], and I hit it with a payload of [33, 44], I wind up with [22, 55] coming out. I've been looking at the javascript tutorials that were recommended on other posts looking for something similar, and I feel like I _could_ get something going, but in a very round-about way. I get a feeling there's a more simple solution that I'm overlooking.

What I'm wanting to do is build an array using .push() and have the items populate in the dashboard dropdown node. Later I'd like to be able to remove items from that dropdown by checking the boxes on those items. I've got the first part working fine, the but removal seems to be less clear.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 April 2021 21:52 UTC](https://discourse.nodered.org/t/removing-items-from-an-array/44716/2 "2021-04-25T21:52:28Z")

</div>

look at [array.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)

e.g.

```auto
var itemToRemove = msg.removeItem;
var array = msg.payload; //assuming payload is an array!
msg.payload = array.filter(e => e != itemToRemove); 
return msg;

```

or if it is an array of items to remove...

```auto
var itemsToRemove = msg.removeItems;
var array = msg.payload; //assuming payload is an array!
msg.payload = array.filter(e => itemsToRemove.includes(e) == false ); 
return msg;

```

Always worth testing in a node shell...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/f/af8cf395985d24fdb9e18dc54d7039999119461a.png)

---

<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:** [25 April 2021 22:41 UTC](https://discourse.nodered.org/t/removing-items-from-an-array/44716/3 "2021-04-25T22:41:31Z")

</div>

This seems to be doing the trick. At least it gives me something to noodle around with. Thank you very much for the lightning fast response.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [27 April 2021 11:34 UTC](https://discourse.nodered.org/t/removing-items-from-an-array/44716/4 "2021-04-27T11:34:35Z")

</div>

Here is something else to think about -- a `change` node using a JSONata expression that returns only items in the `msg.original` array that are not included in the `msg.payload` array:

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

Of course, if your original array is not inside the msg (i.e. in some context variable), the syntax would change a bit -- but the concept is the same... the expression inside square brackets is applied to each element in the outer array, and only those matching are returned.

---

<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:** [30 April 2021 03:47 UTC](https://discourse.nodered.org/t/removing-items-from-an-array/44716/5 "2021-04-30T03:47:31Z")

</div>

Oh my...that Jsonata really is pretty. I should splash around in those waters a bit more.

---

<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:** [14 May 2021 03:48 UTC](https://discourse.nodered.org/t/removing-items-from-an-array/44716/6 "2021-05-14T03:48:26Z")

</div>

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