开发者

Objects within an Array

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",
        开发者_如何学JAVAenglish: "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?


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];
        }
    }
}


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


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


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.


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);
        }
    });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜