Question

How to iterate over an array of literal objects in JavaScript?

I would like to do something like that:

grupo = []; // declare array

text = {}; // declare new object
text.a = "texta"; // declare property "a" of an object.
text.b = "textb";
grupo.push(text); // add object to array

text = {}; // declare new object
text.a = "textc"; // declare property
grupo.push(text); // add object with other property

// Iterate over
for (i=0; i<=grupo.length; i++) {
    console.dir(grupo[i].text.a);
}
Was it helpful?

Solution

There are various errors in that code:

  1. You're putting the same object in the array twice, not putting two objects in the array. After you push text into the array, you're just overwriting the a property on the same object and pushing it again. You haven't created a new object.

  2. You haven't declared any of your variables (everywhere you've said "declare" in your comments, those are not declarations), so you're falling prey to The Horror of Implicit Globals. Use var to declare variables.

  3. A line comment should start with //, not \\ (those cause a syntax error)

  4. The for loop at the end should use <, not <=, for its termination condition. For the various ways to loop through arrays in JavaScript, see this question and its answers.

Here's a cleaned-up version of that code:

var text, grupo, i; // Declare variables

text = {};          // Create an object and assign it to the variable
grupo = [];         // Create an array and assign it to the variable

text.a = "texta";   // Set the property `a` on the object
text.b = "textb";   // Set the property `b` on the object
grupo.push(text);   // Put that object onto the array
text = {};          // Create a second object
text.a = "textc";   // Set the property `a` on that new object
grupo.push(text);   // Put that object on the array

for (i=0;i<grupo.length;i++) {
//         ^-------------------- Note no =
    console.dir(grupo[i].text.a);
}

OTHER TIPS

Do you mean something like this?

for (var key in validation_messages) {
var obj = validation_messages[key];
   for (var prop in obj) {
      // important check that this is objects own property 
      // not from prototype prop inherited
      if(obj.hasOwnProperty(prop)){
        alert(prop + " = " + obj[prop]);
      }
   }
}

Reference: https://stackoverflow.com/a/921808/1054926

groupo[i] is already a text object so you there is an error there. Also, you don't want to look until your index is <= to the length.

Here is a quick look at what you may be looking for in your loop:

for (i=0;i<grupo.length;i++) {  
console.log(i,grupo[i].a);

}

However you will have additional problem when you discover that the value of "a" is not what you may be expecting.

Here another possible "solution"

var text = {};  
var grupo = []; 

text.a = "texta";
text.b = "textb";
grupo.push(text);
text.a = "textc";
grupo.push(text);

for (var i=0;i < grupo.length;i++) {  
    var x = grupo[i];
    if (x && x.a){
        console.log(x.a);        
    } else {
        console.log(x);                
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top