gpt4 book ai didi

JavaScript。从关联数组中提取值

转载 作者:行者123 更新时间:2023-12-05 00:50:45 24 4
gpt4 key购买 nike

如何在 JavaScript 中从这个关联数组中获取值?

我只需要电子邮件地址而不是标签。

(
{
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"
}
]
}

感谢所有输入!

最佳答案

你可能打算做的是:

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])
}
}

//编辑:但是,it's not the best practice用于……在。

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

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。从关联数组中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5721101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com