Javascript。从关联数组中提取值
如何从Javascript中的这个关联数组中获取值? 我只需要电子邮件地址而不是标签。Javascript。从关联数组中提取值
(
{
office = ("[email protected]");
home = ("[email protected]");
work = ("[email protected]");
},
{
home = ("[email protected]");
}
)
UPDATE:在JSON首选输出为:
{
"data": [
{
"email": "[email protected]"
},
{
"email": "[email protected]"
},
{
"email": "[email protected]"
},
{
"email": "[email protected]"
}
] }
感谢所有输入!
您可以通过对象 '的foreach' 得到它的属性:
for(var j = 0; j < mySet.length; j++)
{
for(var propName in mySet[j])
{
var emailAddress = mySet[j][propName];
// Do Stuff
}
}
什么,你可能是指做的是:
var x = [{
office: ("[email protected]"),
home: ("[email protected]"),
work: ("[email protected]")
},
{
home: ("[email protected]")
}]
和:
for(var j = 0; j < x.length; j++)
{
for(var anItem in x[j])
{
console.log(x[j][anItem])
}
}
//编辑: 然而,it's not the best practice用于...英寸
也许你可以在你的数据结构变更为:
var x = [[{
value: "[email protected]",
type: "office"
},
{
value: "[email protected]",
type: "home"
},
{
value: "[email protected]",
type: "work"
}],
[{
value: "[email protected]",
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);
}
}
我无法更改数据结构。我正在Appcelerator中开发一个iPhone应用程序,这是我从地址簿中获取信息时所得到的结果。如果我能以JSON格式获得最终输出(仅限地址),他们最好。这可能吗? – 2011-04-19 19:14:03
目前的输出是:[[“[email protected]”],[“[email protected]”],[“[email protected]”],[“[email protected]”]] in JSON。我会很酷,如果这可能是:{“data”:[{“email”:“[email protected]”},{“email”:“[email protected]”},{“email”:“nothing @ email.com“},{”email“:”[email protected]“}]}。这可能吗? – 2011-04-19 19:18:54
答案编辑的问题:
var ret = {data: []};
for(var j = 0; j < x.length; j++)
{
for(var anItem in x[j])
{
ret.data.push({
email: x[j][anItem]
});
}
}
console.log(ret);
结果保持在ret
。
上面的代码(myObj)发生错误将其更改为mySet,并发出emailAddress警报,但每个警报均为空。哪里不对? – 2011-04-19 18:54:14
对不起,我弄乱了我的符号。我已经更新了它。 – Tejs 2011-04-19 18:57:34
这一次它完美运作!谢谢!是否有可能将所有标签重命名为相同名称,例如电子邮件而不是工作? – 2011-04-19 19:06:11