javascript 将对象转换为数组 angularjs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29003860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:56:20 来源:igfitidea点击:
Convert Object to Array angularjs
提问by Lee Lee
What is the best way to convert:
什么是最好的转换方式:
{
0: 'a',
1: 'b',
2: 'c'
},
{
0: 'd',
1: 'e',
2: 'f'
}
to:
到:
[ [a [b,c] ], [d [e,f] ] ]
where the 0 property
of objects have the array of 1 and 2
objects? Is there a way to do that? Thanks
其中0 property
of 对象具有1 and 2
对象数组?有没有办法做到这一点?谢谢
采纳答案by dfsq
Try to use mapmethod:
尝试使用地图方法:
var data = [{
0: 'a',
1: 'b',
2: 'c'
}, {
0: 'd',
1: 'e',
2: 'f',
3: 'g'
}];
var result = data.map(function (obj) {
var subArr = Object.keys(obj).slice(1).map(function(key) {
return obj[key];
});
return [obj[0], subArr];
});
alert(JSON.stringify(result, null, 4));
Above function use the element at index 0 as the first item of the nested arrays, and everything else in the subarray.
上述函数使用索引 0 处的元素作为嵌套数组的第一项,以及子数组中的所有其他元素。