Code explanation about applied filter

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

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.

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

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.

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

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