I want to cycle through each of the objects in the array, and send out a new message for each one, using some values from the objects to build the new message.
let machines = flow.get("machines") || [];
var iterate = function(id, name) {
node.warn(id);
node.warn(name);
var url = "http://example.com/" + id + "/" + name + "/api";
msg.url = url;
node.send(msg.url);
};
machines.forEach(iterate(msg.id, msg.name));
I'm having trouble passing the array in to the function in this way. Can anyone offer hints on this process?
Show us machines array.
I would also declare your function with const not var, and use let inside your function, rather than var.
[edit]
The first post showed an array of single arrays containing an object,
Your new image shows an array of objects.
With and array of objects it would be.
lovely... this works, thank you. In one example you used 'arr' and in the next you used 'obj'. Am I correct that you could have used 'foo', or anything, but you are using those specifically just for code clarity?
It's a naming convention as the first foreEach was creating an array for each loop so I named it arr,. The second example looped through and was producing an object so I named it obj. That way you know later when you look back or bug fix, you know you expected an array or object.
There could be a object reference issue as forech works on each item and the context is referenced to machines. But I'm not seeing why msgid dissappeared and serial remained. If it is an issue you could clone machines using RED.utils.cloneMessage(machines).ForEach(...
call the api and show us what it returns maybe they changed their output.