0

I have multiple objects like the one below, and I was wondering what the correct syntax would be for putting them all within a single array. I'm also wondering how to correctly cycle through all of the arrays.


var verbMap = [
    {
        infinitive: "gehen",
        thirdPres: "geht",
        thirdPast: "ging",
        aux: "ist",
        pastPart: "gegangen",
        english: "go"
    },
    {
        infinitive: "gelingen",
        thirdPres: "gelingt",
        thirdPast: "gelang",
        aux: "ist",
        pastPart: "gelungen",
        english: "succeed"
    }
];

I know the correct way to cycle through that above array is:


for(v in verbMap){
    for(p in verbMap[v]){
    }
}

If I wanted to cycle through a larger array holding multiple arrays like verbMap, what would be the correct way to do that?

5
  • 3
    Note that those inner "arrays" are in fact objects ({}), not arrays ([]). Commented Oct 5, 2011 at 15:07
  • In JavaScript, everything is an object ;-) Commented Oct 5, 2011 at 15:08
  • 1
    @Zach L.: Yes, but not everything is an array :) Commented Oct 5, 2011 at 15:08
  • 1
    Use a normal for loop for arrays, use for...in for objects. Commented Oct 5, 2011 at 15:08
  • When first learning javascript I also confused the plain object {} to be an associative array (like in php, for example) because you can use the same syntax: someObject["item"]. Instead of thinking of this as an associative array, think of it as a hashtable. It makes a lot more sense since arrays are ordered and object properties are not. Commented Oct 5, 2011 at 15:17

5 Answers 5

4

Just put the verbMap arrays in another array.

var verbMaps = [verbMap1, verbMap2...]

The key thing to understand is that your verbMap is an array of object literals. Only use

for (k in verbMap)...

for object literals.

The correct way to loop thru an array is something like

for (var i = 0; i < verbMaps.length; i++) {
    var currentVerbMap = verbMaps[i];
    for (var j = 0; j < currentVerbMap.length; j++) {
        var currentHash = currentVerbMap[j];
        for (var k in currentHash) {
           console.log(k, currentHash[k];
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

If I wanted to write that out a table, like this: jsfiddle.net/Skooljester/wANRp do you know how I would do that?
@Andrew start by changing the outer loop to use var i;... as outlined in my answer.
1

The following function outputs every value from a (possibly) infinite array given as a parameter.

function printInfiniteArray(value){
    if (value instanceof Array){
        for(i=0;i<value.length;i++){
            printInfiniteArray(value[i]);
        }
    } else {
        console.log(value);
    }
}

Edited code. Thanks jtfairbank

1 Comment

Do not use for...in with arrays in javascript. stackoverflow.com/questions/500504/…
0

Your array does not contain other arrays. It contains objects. You could try this to loop though it.

for(var i = 0; i < verbMap.length; i++)
{
   var obj = verbMap[i];
   alert("Object #"+ i " - infinitive: " + obj.infinitive);
}

Comments

0

You would treat the array like any other javascript object.

var arrayOfArrays = [];
var array1 = ["cows", "horses", "chicken"];
var array2 = ["moo", "neigh", "cock-adoodle-doo"];

arrayOfArrays[0] = array1;
arrayOfArrays[1] = array2;

You can also use javascript's literal notation to create a multi-dimentional array:

var arrayOfArrays = [ ["meat", "veggies"], ["mmmm!", "yuck!"] ];

To cycle through the array of arrays, you'll need to use nested for loops, like so:

for (var i = 0; i < arrayOfArrays.length; i++) {
    var myArray = arrayOfArrays[i];
    for (var j = 0; j < myArray.length; j++) {
        var myData = myArray[0]; // = arrayOfArrays[0][0];
    }
}

DO NOT USE For...in!!! That is not what it was made for. In javascript, For...in can have some unwanted behaviors. See Why is using "for...in" with array iteration a bad idea? for more detail.

Comments

0

You can use jQuery.each to cycle through an array or object, without having to check which one it is. A simple recursive function to cycle through key-value pairs in a nested structure, without knowing the exact depth:

var walk = function(o) {
    $.each(o, function(key, value) {
        if (typeof value == 'object') {
            walk(value);
        } else {
            console.log(key, value);
        }
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.