Pass parts of an array through a function node

Hi, I need some help!

I have an array with many numeric values ​​that go into a function node. I only want to pass a few of these and create a new reduced array. Do you have any tips on how to do this in the best way?

Kind regards
Håkan Bodin

// Get elements from index 5 to 15
const reducedArray = msg.payload.slice(5, 16); // slice(start, end) - end is exclusive

// Or get first 10 elements
const reducedArray = msg.payload.slice(0, 10);

msg.payload = reducedArray;
return msg;

try this

Hi There,

Here's a flow that does this using JSONata:

[{"id":"e0830d1b5f9028b4","type":"inject","z":"aac9e086f90b95e9","name":"","props":[{"p":"array","v":"[1,2,3,4,5,6,7]","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":601.4285714285713,"y":662.8571428571428,"wires":[["6de3793d948ab250"]]},{"id":"6de3793d948ab250","type":"change","z":"aac9e086f90b95e9","name":"","rules":[{"t":"set","p":"values","pt":"msg","to":"$filter($$.array, function($v,$idx) { $idx >= 1 and $idx <= 3 })","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":814.2857666015625,"y":727.1429080963135,"wires":[["69c6dd835e7b9890"]]},{"id":"69c6dd835e7b9890","type":"debug","z":"aac9e086f90b95e9","name":"debug 463","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":true,"targetType":"full","statusVal":"","statusType":"auto","x":1017.1428833007812,"y":800,"wires":[]}]

The key here is the JSONata stanza in the change node: $filter($$.array, function($v,$idx) { $idx >= 1 and $idx <= 3 }) - that reduces the array from [1,2,3,4,5,6,7] to [2,3,4].

The filter condition, in this case, uses the index of the element but it can also use the value of the element - the $v of the filter function.

The benefit of using JSONata is that there are less function nodes and Javascript code in your flows. The dark side is that you have to learn JSONata syntax and concepts --> jsonata.org --> it has similarities to Javascript but also XSLT.

But JSONata is really well integrated into Node-RED, having also a test panel in the JSONata editor:

And documentation on each function that is available:

Haha I look at it completely opposite, but glad you pointed out. If you learn to write js, you are speaking a truly universal language. And further, it will be closely related to other programming languages, making any transition in the future easier. I know js fluently, but JSONata gives me headache…

When I need complex filtering of arrays, I do it a bit more verbose like this:

const newArray = [];
msg.payload.forEach((item) => {
  if(item){ // put your filter logic inside if here
    newArray.push(item);
  }
});
msg.payload = newArray;

You are speaking a object-oriented-like functional interpreted language, far from universal.

If you only learn a single language, then you will miss out on concepts such as list oriented programming (lisp), declarative programming language (prolog + erlang) and procedural stuff like Ada or Cobol even!

JSONata is a transformational language with its own quirks but as a concept, well further understanding and perhaps you'll need it later.

Sorry had to correct you there, there is no universal language, there is only the right language for the problem at hand.

EDIT:

Sorry have to get this example off my chest - even if its way off topic :wink:

So having created Erlang-Red - Node-RED backend in Erlang - I've come to enjoy coding Erlang. One thing Erlang does incredibly well is recovery: processes are automatically restarted if necessary. So if something fails, the system can recover, creating self-healing systems.

For this, Erlang has the Supervisor behaviour. All the supervisor does is monitor processes and if they fail, it restarts them or not - depending on the configuration. That's their single purpose. Erlang is many lightweight processes running concurrently communicating via messages. So processes failing does not affect other processes.

In created Erlang-Red, I was faced with the question: do I implement Supervisors as nodes? So that I could visually configure recovery of processes. I was torn between staying true to Node-RED and extending it with Erlang specific features. In the end, I created the supervisor node. These do nothing in Node-RED but in Erlang-Red they do exactly what they are meant to do: restart other nodes that they are supervising. A supervisor is somewhat as a catch node but in reverse: it actively controls the lifecycle of a node.

The point of the story? Without trying out Erlang, I would never have developed the supervisor node. I'm now thinking of how to transport the idea of a supervisor node to Node-RED, how would that be implemented in NodeJS? No idea. But it would definitely be something very useful for creating systems with Node-RED.

I don't think that is correct either. Very often there is a choice of languages with no clear 'right' one to use for a particular problem.

1 Like

Well yes and no, the choice becomes simpler the more experience one has.

Sure there is no right language for the problem - unless someone creates a programming language for the problem - which often does happens (check Domain Specific Languages in Ruby).

Ironically my experience has been, as one discovers more of the problem, the original selection of language might actually have been wrong - but by that time, there is already too much code and no going back.

This also happens when the new "hot" languages come along - think of NodeJS in the beginning: everyone left Php and Python for NodeJS. Think Ruby: every left Java and Python for it. Even Elixir is an interesting case in point. Currently its Rust and Go. I recently read Php is making a come back :wink:

With out examples of how you wish to reduce the array an example is difficult to offer.

You could possible do this with a nodes split > switch (recreate message sequence) > join (automatic) . This would be the low code option.

Hi,

In this case, hipatel93's solution was perfect because the values ​​I wanted to pass on were in a consecutive order in the middle of my array.

// Hobbe

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