# How to remove empty value in array using change node

**URL:** <https://discourse.nodered.org/t/how-to-remove-empty-value-in-array-using-change-node/33911>\
**Category:** General\
**Created:** [8 October 2020 02:51 UTC](https://discourse.nodered.org/t/how-to-remove-empty-value-in-array-using-change-node/33911 "2020-10-08T02:51:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [8 October 2020 02:51 UTC](https://discourse.nodered.org/t/how-to-remove-empty-value-in-array-using-change-node/33911/1 "2020-10-08T02:51:55Z")

</div>

I have a text file that contains an empty line like this:

```auto

a

b
c

d

e

f

```

When reading the file I want the resulting array to get rid of the empty values using the `$filter` of the `change` node.  
[https://docs.jsonata.org/higher-order-functions#filter](https://docs.jsonata.org/higher-order-functions#filter)  
..but not working, i don't know what went wrong?  
Please tell me. Thanks!

![node-change-filter](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/3/732068a95063c95c0b9421ed3224956c49050efe.png)

```auto
[{"id":"8d9ec817.efd0b8","type":"inject","z":"f1e299b0.7d26b8","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":1600,"wires":[["8926455e.3b12c8"]]},{"id":"8926455e.3b12c8","type":"file in","z":"f1e299b0.7d26b8","name":"","filename":"/Temp/File1.txt","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":380,"y":1600,"wires":[["b5a982d9.951d2"]]},{"id":"1fe3fff6.16cab","type":"debug","z":"f1e299b0.7d26b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":870,"y":1600,"wires":[]},{"id":"b5a982d9.951d2","type":"change","z":"f1e299b0.7d26b8","name":"$split(msg.payload,'\\n')","rules":[{"t":"set","p":"srcArray","pt":"msg","to":"$split(msg.payload,'\\n')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":1600,"wires":[["c2c03026.b1631"]]},{"id":"c2c03026.b1631","type":"change","z":"f1e299b0.7d26b8","name":"$filter","rules":[{"t":"set","p":"destArray","pt":"msg","to":"$filter(srcArray, function($v) {\t 0 < $length($v)\t})","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":1600,"wires":[["1fe3fff6.16cab"]]}]

```

---

<div class="post-metadata">

**Author:** ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)\
**Post date:** [8 October 2020 03:11 UTC](https://discourse.nodered.org/t/how-to-remove-empty-value-in-array-using-change-node/33911/2 "2020-10-08T03:11:12Z")

</div>

In trying your code, I copied the file content you included. I noticed that the 'lines' actually appear to be tabs. When I changed the "\n" to "\t", the code worked as expected. So maybe your file actually has tabs in it, not newlines.

If you want to handle newlines or tabs, you could use a regular expression instead:

```auto
$split(payload, /[\n\t]/)

```

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [8 October 2020 03:51 UTC](https://discourse.nodered.org/t/how-to-remove-empty-value-in-array-using-change-node/33911/3 "2020-10-08T03:51:26Z")

</div>

I use Windows, and in the file content is newlines, not tabs...

![node-change-filter-1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/2/a2f8505acd85978c0a390dd0ad2a292aaf1ecbde.png)

I tried `$split(payload, /[\n\t]/)` but still doesn't work ☹

---

<div class="post-metadata">

**Author:** ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)\
**Post date:** [8 October 2020 04:04 UTC](https://discourse.nodered.org/t/how-to-remove-empty-value-in-array-using-change-node/33911/4 "2020-10-08T04:04:18Z")

</div>

Hm, it works for me, but I'm on Ubuntu. Given you're on Windows, they are probably carriage returns, so try "\r". Although Windows normally has line ends as "\n\r".

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [8 October 2020 04:11 UTC](https://discourse.nodered.org/t/how-to-remove-empty-value-in-array-using-change-node/33911/5 "2020-10-08T04:11:09Z")

</div>

I also tried "\n\r", "/(\r\n|\n|\r)/" but still doesn't work.  
I'm trying with `$replace`

---

<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:** [8 October 2020 07:49 UTC](https://discourse.nodered.org/t/how-to-remove-empty-value-in-array-using-change-node/33911/6 "2020-10-08T07:49:51Z")

</div>

Could we see the output of the debug message after you split the file text? Not a screenshot

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [8 October 2020 08:08 UTC](https://discourse.nodered.org/t/how-to-remove-empty-value-in-array-using-change-node/33911/7 "2020-10-08T08:08:04Z")

</div>

> [@michaelblight](#):
>
> `$split(payload, /[\n\t]/)`

If I read the following :

- [python - Regular Expression to match cross platform newline characters - Stack Overflow](https://stackoverflow.com/questions/1331815/regular-expression-to-match-cross-platform-newline-characters)

then for PC (windows) the regex to match newline characters should be `\r\n` instead of `\n`

---

<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:** [7 December 2020 08:08 UTC](https://discourse.nodered.org/t/how-to-remove-empty-value-in-array-using-change-node/33911/8 "2020-12-07T08:08:05Z")

</div>

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