-2

我有一个这种形式的json:

 var test = { "name1" : [ "aaa", "bbb"], "name2" : [ "ccc", "ddd" ] };

我试图像这样迭代

for (var names in test )
{
    for ( var element in names )
    {
        // problem we got the chars in the "names1", etc
    }
}

我到底错过了什么?

4

1 回答 1

1

要迭代test对象,您需要这样的东西:

for(var key in test) //iterate object keys
{
    var elements = test[key];
    for(var i = 0; i < elements.length; i++) //iterate array indexes
    {
        var element = elements[i];
    }
}
于 2013-09-07T14:41:54.053 回答