Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterating through object

I'm having a really hard time trying to find a way to iterate through this object in the way that I'd like. I'm using only Javascript here.

First, here's the object

{ "dialog": {     "dialog_trunk_1":{         "message": "This is just a JSON Test"     },          "dialog_trunk_2":{         "message": "and a test of the second message"     },      "dialog_trunk_3":     {         "message": "This is a test of a bit longer text. Hopefully this will at the very least create 3 lines and trigger us to go on to another box. So we can test multi-box functionality, too."     } } } 

Right now, I'm just trying basic ways to get through to each dialog_trunk on this object. I ideally want to loop through the object and for each trunk, display it's message value.

I've tried using a for loop to generate the name/number of the dialog_trunk on the fly, but I can't access the object using a string for the object name so I'm not sure where to go from here.

like image 954
Eric Avatar asked Oct 11 '13 17:10

Eric


People also ask

Can we iterate an object?

There are two methods to iterate over an object which are discussed below: Method 1: Using for…in loop: The properties of the object can be iterated over using a for..in loop. This loop is used to iterate over all non-Symbol iterable properties of an object.

How do you loop through all properties of an object?

Object.key(). It returns the values of all properties in the object as an array. You can then loop through the values array by using any of the array looping methods.

How do you iterate through an object in react?

Use the Object. keys() method to get an array of the object's keys. Use the map() method to iterate over the array of keys.

How do I iterate an object in node JS?

Since Javascript 1.7 there is an Iterator object, which allows this: var a={a:1,b:2,c:3}; var it=Iterator(a); function iterate(){ try { console. log(it. next()); setTimeout(iterate,1000); }catch (err if err instanceof StopIteration) { console.


2 Answers

You use a for..in loop for this. Be sure to check if the object owns the properties or all inherited properties are shown as well. An example is like this:

var obj = {a: 1, b: 2}; for (var key in obj) {   if (obj.hasOwnProperty(key)) {     var val = obj[key];     console.log(val);   } } 

Or if you need recursion to walk through all the properties:

var obj = {a: 1, b: 2, c: {a: 1, b: 2}}; function walk(obj) {   for (var key in obj) {     if (obj.hasOwnProperty(key)) {       var val = obj[key];       console.log(val);       walk(val);     }   } } walk(obj); 
like image 87
Deathspike Avatar answered Sep 20 '22 16:09

Deathspike


My problem was actually a problem of bad planning with the JSON object rather than an actual logic issue. What I ended up doing was organize the object as follows, per a suggestion from user2736012.

{ "dialog": {     "trunks":[     {         "trunk_id" : "1",         "message": "This is just a JSON Test"     },     {         "trunk_id" : "2",         "message": "This is a test of a bit longer text. Hopefully this will at the very least create 3 lines and trigger us to go on to another box. So we can test multi-box functionality, too."     }     ] } } 

At that point, I was able to do a fairly simple for loop based on the total number of objects.

var totalMessages = Object.keys(messages.dialog.trunks).length;      for ( var i = 0; i < totalMessages; i++)     {         console.log("ID: " + messages.dialog.trunks[i].trunk_id + " Message " + messages.dialog.trunks[i].message);     } 

My method for getting totalMessages is not supported in all browsers, though. For my project, it actually doesn't matter, but beware of that if you choose to use something similar to this.

like image 41
Eric Avatar answered Sep 19 '22 16:09

Eric