I am playing with node red and the unifi palette. I queried the unifi controller for the connected devices and it gives me back an array with 22 objects. In the object, I'm interested in pulling the mac and sending it as a separate msg so I can check it against a list of macs I'm interested in.
I've been playing with this for a couple hours and the best I have come up with is to display the individual mac in the debugger window using node.warn().
var l = msg.payload[0].length;
for (var i = 0; i < l; i++) {
node.warn(msg.payload[0][i].mac);
}
The Node-RED documentation describes how to return multiple messages from a Function node.
You can either build an array of the messages you want to send and then return that. Or use node.send(...)
in your loop to send each in turn.
For example:
var l = msg.payload[0].length;
for (var i = 0; i < l; i++) {
node.send({
payload: msg.payload[0][i].mac
});
}
Whilst the Function node lets you write it all in JavaScript, it is also worth looking at the other core nodes to see how they can help remove the need to write code.
You could also acheive the same result by using a sequence of nodes such as:
msg.payload[0]
to msg.payload
msg.payload.mac
to msg.payload