gpt4 book ai didi

javascript - JS从对象中获取数组

转载 作者:行者123 更新时间:2023-11-30 15:33:16 25 4
gpt4 key购买 nike

Input-
{
"0": {
"NAME": "ABC"
},
"1": {
"NAME": "DEF"
},
"2": {
"NAME": "GHI"
},
"3": {
"NAME": "JKL"
},
"4": {
"NAME": "MNO"
}
}

我有这个输入。我只想获得像这样的数组格式的名称 Output-

["ABC", "DEF", "GHI", "JKL", "MNO"].

尝试

var arr =[]; for( var i in data ) { if (data.hasOwnProperty(i)){ arr.push(data[i]); } } 

最佳答案

遍历对象的键并将其NAME属性映射到一个数组中。

var input = { "0": { "NAME": "ABC" }, "1": { "NAME": "DEF" }, "2": { "NAME": "GHI" }, "3": { "NAME": "JKL" }, "4": { "NAME": "MNO" } };

var result = Object.keys(input).map(function(key){
return input[key].NAME;
});

console.log(result);

关于javascript - JS从对象中获取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41950644/

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