JavaScript.从关联数组中提取值 [英] JavaScript. Extract values from associative array
本文介绍了JavaScript.从关联数组中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从JavaScript中的此关联数组中获取值?
How can I get the values from this associative array in JavaScript?
我只需要电子邮件地址,而不需要标签.
I just need the email addresses and not the labels.
(
{
office = ("my@email.com");
home = ("ahome@anotheremail.com");
work = ("nothing@email.com");
},
{
home = ("test@test.se");
}
)
更新:JSON中的首选输出为:
UPDATE: Prefered output in JSON would be:
{
"data": [
{
"email": "my@email.com"
},
{
"email": "ahome@anotheremail.com"
},
{
"email": "nothing@email.com"
},
{
"email": "test@test.se"
}
]
}
感谢所有输入!
推荐答案
您可能打算做的是:
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);
}
}
这篇关于JavaScript.从关联数组中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文