# Code explanation about applied filter

**URL:** <https://discourse.nodered.org/t/code-explanation-about-applied-filter/43709>\
**Category:** General\
**Created:** [5 April 2021 13:59 UTC](https://discourse.nodered.org/t/code-explanation-about-applied-filter/43709 "2021-04-05T13:59:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Auke](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Auke](https://discourse.nodered.org/u/Auke)\
**Post date:** [5 April 2021 13:59 UTC](https://discourse.nodered.org/t/code-explanation-about-applied-filter/43709/1 "2021-04-05T13:59:01Z")

</div>

Hi,  
I have NR-node-MongoDB that feeds a function "Wrapper" with msg.payload.  
Since i am new to NR i am not sure what the first part of the function "Wrapper" does: it uses some filter logic but i cannot find the exact purpose of it

```auto
var date=msg.req.params.date;
var enddate = new Date(msg.req.params.date);
enddate.setDate(enddate.getDate()+1);

var dataset = msg.payload.filter(x=>{ return x.startDate < date; });

msg.payload=[dataset[dataset.length-1]].concat(
    msg.payload.filter(x=>{return x.startDate==date;})
);

```

I cannot find the syntax of filter in the NR tutorial. Can anyone pinpoint me to the NR filter function? An url is also okay.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [5 April 2021 14:05 UTC](https://discourse.nodered.org/t/code-explanation-about-applied-filter/43709/2 "2021-04-05T14:05:45Z")

</div>

Hi @Auke , welcome to the forum.

I've edited your post to wrap the code with ``` so that it gets formatted correctly and is easier to read.

It looks like your `msg.payload` is an Array type object, so the filter function in question is this one: [Array.prototype.filter() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)

The function you provide to `filter` is called for each element in the array. If the function returns true, the value is kept. Otherwise it is filtered out.

In this instance, that means `dataset` ends up being an array of elements whose `startDate` property is less than `date`.

It then goes on to set `msg.payload` to be an array containing the last element of `dataset`, plus all elements from the original payload whose `startDate` equals `date`.

---

<div class="post-metadata">

**Author:** ![Auke](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Auke](https://discourse.nodered.org/u/Auke)\
**Post date:** [5 April 2021 14:22 UTC](https://discourse.nodered.org/t/code-explanation-about-applied-filter/43709/3 "2021-04-05T14:22:24Z")

</div>

Thanks @knolleary this gives me a good starter of understanding the NR function, i did not share everything but this certainly helps.  
Auke

---

<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:** [4 June 2021 14:22 UTC](https://discourse.nodered.org/t/code-explanation-about-applied-filter/43709/4 "2021-06-04T14:22:53Z")

</div>

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