Looping through an object by incrementing an object name

I get a JSON variable with sequentially numbered objects, and need to loop through it to pull out some data.

In the past, I've had success with ["name" + i], but NR is calling it an unknown object in this case.

Anyone have an idea how I can iterate through this?



var targets = flow.get('targets');
var unitCount = Object.keys(targets).length;
var position = " ";


for (i = 1; i < unitCount; i++) {
    position = 'xds' + i;
    msg.ip = targets.position.IP;
    msg.label = targets.position.Unit;
    msg.position = position;
    msg.index = i;
    node.send(msg);
}

msg.targets = targets;
msg.vars = targets.length;
return msg;



This is the JSON data entered to the flow variable:

{
    "xds1": {
        "IP": "10.X.X.X",
        "Unit": "R1"
    },
    "xds2": {
        "IP": "10.X.X.X",
        "Unit": "S1"
    },
    "xds3": {
        "IP": "10.X.X.X",
        "Unit": "S2"
    },
    "xds4": {
        "IP": "10.X.X.X",
        "Unit": "A1"
    },
    "xds5": {
        "IP": "10.X.X.X",
        "Unit": "A2"
    }
}

As an example of this working, though in different use, I've used the "string + i" method to create new strings on each iteration, using it to then get a flow variable. I didn't expect it to work because it never worked in other languages I've used, but it did here, so I was hoping to get away with it on the JSON object also...

image

Look at the Object functions keys, values and entries for iterating object properties.

MDN is super helpful here as it has interactive examples you can try out in the browser

Awesome, thanks Steve, will try.

Ok, that got me on the right path, though initially, I couldn't get it to iterate into the subkeys(?). With a boatload of trial and error, then stumbling across something on Stack Exch, I'm able to iterate through.

Posting for anyone that finds this and needs a solution.

var targets = flow.get('targets');
var keys = Object.keys(targets);
var data = {}

for(var i = 0; i < keys.length;i++){
    data = targets[keys[i]]
    msg.test = data.IP;
    msg.test2 = data.Unit;
    node.send(msg);
}

Or you could have used a split node and a change node to iterate over the object and manipulate the msg properties.
e.g.

[{"id":"07a8af26d48806c8","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{     \"xds1\": {         \"IP\": \"10.X.X.X\",         \"Unit\": \"R1\"     },     \"xds2\": {         \"IP\": \"10.X.X.X\",         \"Unit\": \"S1\"     },     \"xds3\": {         \"IP\": \"10.X.X.X\",         \"Unit\": \"S2\"     },     \"xds4\": {         \"IP\": \"10.X.X.X\",         \"Unit\": \"A1\"     },     \"xds5\": {         \"IP\": \"10.X.X.X\",         \"Unit\": \"A2\"     } }","payloadType":"json","x":570,"y":5200,"wires":[["b7db0f3b0e1dd5f0"]]},{"id":"b7db0f3b0e1dd5f0","type":"split","z":"d1395164b4eec73e","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":710,"y":5200,"wires":[["2753693fbeb19e3b"]]},{"id":"2753693fbeb19e3b","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"test1","pt":"msg","to":"payload.IP","tot":"msg"},{"t":"set","p":"test2","pt":"msg","to":"payload.Unit","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":860,"y":5200,"wires":[["3552c41dbd6cc181"]]},{"id":"3552c41dbd6cc181","type":"debug","z":"d1395164b4eec73e","name":"debug 2476","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1050,"y":5200,"wires":[]}]

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