gpt4 book ai didi

javascript - 如何检索 Javascript 对象名称数组

转载 作者:行者123 更新时间:2023-11-28 04:23:28 25 4
gpt4 key购买 nike

我正在尝试检索汽车名称(即“Ford feigo”、“Nissan”、“Duster”)。我试图通过 carsinfo["0"].name 检索它们以获取汽车名称,但它给了我 未定义 作为返回。

var carsinfo =    {"cars": [
{
"Ford feigo": {
"milege": "11",
"color": "White",
"electric": yes
}
},
{
"Nissan": {
"milege": "33",
"color": "Black",
"electric": 74
}
},
{
"Duster": {
"milege": "26",
"color": "RED",
"electric": 87
}
}
],
};

最佳答案

您可以使用 Object.keysArray#map 检索它们。

let carsinfo = {
"cars": [{
"Ford feigo": {
"milege": "11",
"color": "White",
"electric": 'yes'
}
},
{
"Nissan": {
"milege": "33",
"color": "Black",
"electric": 74
}
},
{
"Duster": {
"milege": "26",
"color": "RED",
"electric": 87
}
}
],
};

let cars = [].concat(...carsinfo.cars.map(Object.keys));

console.log(cars);

关于javascript - 如何检索 Javascript 对象名称数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45259457/

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