简体   繁体   English

如何在Javascript中将索引关联数组的数组转换为逗号分隔的关联数组

[英]How to convert Array of Indexed Associative Array to Comma Separated Associative Array in Javascript

I have an indexed Associative Array like 我有一个索引关联数组

[
0:{abc: 123, xyz: 456, foo: null, bar: 0}
1:{abc: 235, xyz: 556, foo: null, bar: 0}
]

now how to convert it to comma separated form 现在如何将其转换为逗号分隔的形式

[{abc: 123, xyz: 456, foo: null, bar: 0},{abc: 235, xyz: 556, foo: null, bar: 0}
    ]

You could use Object.assign with an array as target object. 您可以将Object.assign与数组一起用作目标对象。

 var data = { 0: { abc: 123, xyz: 456, foo: null, bar: 0 }, 1: { abc: 235, xyz: 556, foo: null, bar: 0 } }, array = Object.assign([], data); console.log(array); 

Or just Object.values if the index does not matter. 如果索引无关紧要,也可以只是Object.values

 var data = { 0: { abc: 123, xyz: 456, foo: null, bar: 0 }, 1: { abc: 235, xyz: 556, foo: null, bar: 0 } }, array = Object.values(data); console.log(array); 

如果第一个变量是对象而不是数组,则可以执行以下操作:

object = Object.keys(object).map(key => object[key]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM