I need to get array of objects from array of strings. For examaple:
我需要从字符串数组中获取对象数组。例如:
var arr = ["1005", "1005", "1005", "1006", "1006", "1006", "1007", "1007"];
var result = arr.reduce((iss, index) => {
iss[index] = (iss[index] || 0) + 1;
return iss
}, {});
and the result would be
结果将是
{1005: 3, 1006: 3, 1007: 2}
So is there a way to get next output:
那么有没有办法获得下一个输出:
[{"1005":3},{"1006":3},{"1007":2}]
2 个解决方案
#1
3
If you really want that:
如果你真的想要那个:
result = Object.entries(result).map(([key, value]) => ({[key]: value}));
#2
0
You can extend result
by iterating its keys
您可以通过迭代其键来扩展结果
result = Object.keys ( result ).map( s => ({ [s] : result[s] }) );
#1
3
If you really want that:
如果你真的想要那个:
result = Object.entries(result).map(([key, value]) => ({[key]: value}));
#2
0
You can extend result
by iterating its keys
您可以通过迭代其键来扩展结果
result = Object.keys ( result ).map( s => ({ [s] : result[s] }) );