JavaScript。从关联数组中提取值
如何在JavaScript中从这个关联数组中获取值 我只需要电子邮件地址,不需要标签JavaScript。从关联数组中提取值,javascript,arrays,Javascript,Arrays,如何在JavaScript中从这个关联数组中获取值 我只需要电子邮件地址,不需要标签 ( { office = ("my@email.com"); home = ("ahome@anotheremail.com"); work = ("nothing@email.com"); }, { home = ("test@test.se"); } ) 更新:首选JSON输出: { "data": [ { "
(
{
office = ("my@email.com");
home = ("ahome@anotheremail.com");
work = ("nothing@email.com");
},
{
home = ("test@test.se");
}
)
更新:首选JSON输出:
{
"data": [
{
"email": "my@email.com"
},
{
"email": "ahome@anotheremail.com"
},
{
"email": "nothing@email.com"
},
{
"email": "test@test.se"
}
]
}
感谢所有的投入 您可以在对象上“foreach”以获取其属性:
for(var j = 0; j < mySet.length; j++)
{
for(var propName in mySet[j])
{
var emailAddress = mySet[j][propName];
// Do Stuff
}
}
for(var j=0;j
您可能想做的是:
var x = [{
office: ("my@email.com"),
home: ("ahome@anotheremail.com"),
work: ("nothing@email.com")
},
{
home: ("test@test.se")
}]
以及:
并使用以下命令进行迭代:
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);
}
}
for(变量i=0,xlength=x.length;i
编辑问题的答案:
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);
var ret={data:[]};
对于(var j=0;j
结果保存在ret
以下是一行:
console.log(Object.keys(assoc).map(k => assoc[k]));
其中,assoc
是关联数组
编辑
我有一个更好的答案。是您以JSON格式输入的吗?因为如果是这样,那就是错误的语法。然而
let _in = [
{
office : "my@email.com",
home : "ahome@anotheremail.com",
work : "nothing@email.com",
},
{
home : "test@test.se"
}
]
let _out = []
_in.forEach( record => {
_out = _out.concat(Object.values(record).map(x => new Object({email : x})))
})
console.log(_out)
对于每个记录,我提取了值并用“email”属性“打包”到一个对象中,然后我合并了从原始记录数组中获得的所有数组,这似乎是您要查找的。我发现上面的代码有一个错误(myObj)将其更改为mySet并发出emailAddress警报,但每个警报都是空的。怎么了?这次效果很好!谢谢是否可以将所有标签重命名为相同的名称,例如“电子邮件”而不是“工作”?您可以将这些值转换为您选择的另一个对象。当前输出为:[”my@email.com"],["ahome@anotheremail.com"],["nothing@email.com"],["test@test.s-e“]]在JSON中。如果这是:{“数据”:[{“电子邮件”:”,我会很酷my@email.com},{“电子邮件”:ahome@anotheremail.com},{“电子邮件”:nothing@email.com},{“电子邮件”:test@test.se"}]}. 这可能吗?我无法更改数据结构。我正在Appcelerator中开发一个iPhone应用程序,这是我从内置地址簿请求信息时得到的。如果我能得到JSON格式的最终输出(仅限地址),它们将是最好的。这可能吗?电流输出现在为:[“my@email.com"],["ahome@anotheremail.com"],["nothing@email.com"],["test@test.se“]]在JSON中。如果这是:{“数据”:[{“电子邮件”:”,我会很酷my@email.com},{“电子邮件”:ahome@anotheremail.com},{“电子邮件”:nothing@email.com},{“电子邮件”:test@test.se"}]}. 这可能吗?
console.log(Object.keys(assoc).map(k => assoc[k]));
let _in = [
{
office : "my@email.com",
home : "ahome@anotheremail.com",
work : "nothing@email.com",
},
{
home : "test@test.se"
}
]
let _out = []
_in.forEach( record => {
_out = _out.concat(Object.values(record).map(x => new Object({email : x})))
})
console.log(_out)