# Looping through an object by incrementing an object name

**URL:** <https://discourse.nodered.org/t/looping-through-an-object-by-incrementing-an-object-name/84929>\
**Category:** General\
**Created:** [26 January 2024 00:07 UTC](https://discourse.nodered.org/t/looping-through-an-object-by-incrementing-an-object-name/84929 "2024-01-26T00:07:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rgrove](https://avatars.discourse-cdn.com/v4/letter/r/5f8ce5/32.png) [@Rgrove](https://discourse.nodered.org/u/Rgrove)\
**Post date:** [26 January 2024 00:07 UTC](https://discourse.nodered.org/t/looping-through-an-object-by-incrementing-an-object-name/84929/1 "2024-01-26T00:07:45Z")

</div>

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?

```auto

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:

```auto
{
    "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"
    }
}

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/9/297ed96ed7cb5957c939b5189837903ab2de1544.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b2ccf1fae6a3c500d505d3ede83f8fd569b9911f.png)

---

<div class="post-metadata">

**Author:** ![Rgrove](https://avatars.discourse-cdn.com/v4/letter/r/5f8ce5/32.png) [@Rgrove](https://discourse.nodered.org/u/Rgrove)\
**Post date:** [26 January 2024 00:14 UTC](https://discourse.nodered.org/t/looping-through-an-object-by-incrementing-an-object-name/84929/2 "2024-01-26T00:14:03Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1e47b613e3856898d688c3cfce1a9742bdff6f65.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 January 2024 00:31 UTC](https://discourse.nodered.org/t/looping-through-an-object-by-incrementing-an-object-name/84929/3 "2024-01-26T00:31:06Z")

</div>

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

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

> **[Object.keys() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys)**
>
> The Object.keys() static method returns an array of a given object's own enumerable string-keyed property names.

> **[Object.entries() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries)**
>
> The Object.entries() static method returns an array of a given object's own enumerable string-keyed property key-value pairs.

> **[Object.values() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values)**
>
> The Object.values() static method returns an array of a given object's own enumerable string-keyed property values.

---

<div class="post-metadata">

**Author:** ![Rgrove](https://avatars.discourse-cdn.com/v4/letter/r/5f8ce5/32.png) [@Rgrove](https://discourse.nodered.org/u/Rgrove)\
**Post date:** [26 January 2024 02:20 UTC](https://discourse.nodered.org/t/looping-through-an-object-by-incrementing-an-object-name/84929/4 "2024-01-26T02:20:38Z")

</div>

Awesome, thanks Steve, will try.

---

<div class="post-metadata">

**Author:** ![Rgrove](https://avatars.discourse-cdn.com/v4/letter/r/5f8ce5/32.png) [@Rgrove](https://discourse.nodered.org/u/Rgrove)\
**Post date:** [26 January 2024 03:20 UTC](https://discourse.nodered.org/t/looping-through-an-object-by-incrementing-an-object-name/84929/5 "2024-01-26T03:20:00Z")

</div>

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.

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 January 2024 07:19 UTC](https://discourse.nodered.org/t/looping-through-an-object-by-incrementing-an-object-name/84929/6 "2024-01-26T07:19:12Z")

</div>

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

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 February 2024 07:19 UTC](https://discourse.nodered.org/t/looping-through-an-object-by-incrementing-an-object-name/84929/7 "2024-02-09T07:19:13Z")

</div>

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