且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JavaScript.从关联数组中提取值

更新时间:2023-02-26 09:20:34

您可能打算做的是:

var x = [{
 office: ("my@email.com"),
 home: ("ahome@anotheremail.com"),
 work: ("nothing@email.com")
},
{
 home: ("test@test.se")
}]

和:

for(var j = 0; j < x.length; j++)
{
    for(var anItem in x[j])
    {
        console.log(x[j][anItem])
    }
}

//但是,用于…in并非***做法.

也许您可以将数据结构更改为:

Maybe you could change your data structure to:

var x = [[{
        value: "my@email.com",
        type: "office"
    },
    {
        value: "ahome@anotheremail.com",
        type: "home"
    },
    {
        value: "nothing@email.com",
        type: "work"
    }],
    [{
        value: "test@test.se",
        type: "home"
    }]];

并使用进行迭代:

for( var i = 0, xlength = x.length; i < xlength; i++ )
{
    for( var j=0, ylength = x[i].length; j < ylength; j++ )
    {
        console.log(x[i][j].value);
    }
}