![](/img/trans.png)
[英]How to convert an array of objects into a single object using reduce?
[英]How to convert array into array of objects using .reduce()
如何使用.reduce()将多维数组转换为对象数组?
起始数组
[
[
['juice', 'apple'], ['maker', 'motts'], ['price', 12]
],
[
['juice', 'orange'], ['maker', 'sunkist'], ['price', 11]
]
]
和结束数组
[
{juice: 'apple', maker: 'motts', price: 12},
{juice: 'orange', maker: 'sunkist', price: 11}
]
这就是我现在所拥有的。 这真的只是我在黑暗中拍摄。
var transformData = (array) => {
var newArray = array.push(function (all, item, index) {
var newItem = item.reduce(function (all, item, index) {
all[item[0]] = item[1]
return all
}, {})
return all
}, [])
newArray.push(newItem)
return newArray
}
您可以尝试的组合array.map
和array.reduce
。
Array.push
适合将元素添加到数组中,但是如果要将数组的所有元素转换为特定规范,最好使用array.map
var data = [ [ ['juice', 'apple'], ['maker', 'motts'], ['price', 12] ], [ ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11] ] ] var result = data.map(function(list){ return list.reduce(function(o, kv){ o[kv[0]] = kv[1]; return o; }, {}); }) console.log(result)
您可以将Array#map
与Object.assign
结合使用,以具有扩展语法...
的属性。
var data = [[['juice', 'apple'], ['maker', 'motts'], ['price', 12]], [['juice', 'orange'], ['maker', 'sunkist'], ['price', 11]]], result = data.map(o => Object.assign(...o.map(p => ({ [p[0]]: p[1] })))); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
这是使用reduce
和foreach
的解决方案。
var items = [ [ ['juice', 'apple'], ['maker', 'motts'], ['price', 12] ], [ ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11] ] ]; var transformData = items.reduce((newArr, currArr) => { var obj = {}; currArr.forEach((x) => { obj[x[0]] = x[1]; }); return newArr.concat(obj); },[]); console.log(transformData);
一个解决方案使用两个reduce。
var items = [ [ ['juice', 'apple'], ['maker', 'motts'], ['price', 12] ], [ ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11] ] ]; var transformData = items.reduce((newArr, currArr) => { return newArr.concat(currArr.reduce((o, arr) =>{ o[arr[0]] = arr[1]; return o; }, {})); },[]); console.log(transformData);
您实际上已经关闭了,只是您似乎认为array.push的工作方式类似于array.reduce-事实并非如此
var transformData = array =>
array.map(subarray =>
subarray.reduce((result, [key, value]) => {
result[key] = value;
return result;
}, {})
);
由于您使用的是ES2015 +的某些功能,因此我使用了更多的方法...内部数组[键,值]的结构分解
我只需要将此“扩展名”添加到到目前为止的最佳答案
const transformData=_=>_.map(_=>Object.assign(..._.map(([$,_])=>({[$]:_}))));
尝试在几个月内阅读,然后知道它在做什么:p
尝试这个:
var parray=[
[
['juice', 'apple'], ['maker', 'motts'], ['price', 12]
],
[
['juice', 'orange'], ['maker', 'sunkist'], ['price', 11]
]
];
var newArray=[]
parray.forEach(function(data){
var cak = data.reduce(function(a, b) {
a[b[0]]=b[1]
return a;
}, {})
newArray.push(cak)
})
console.log(newArray)
这是一个使用map
和reduce
以及对象传播语法的简洁表达式。
const data = [ [ ['juice', 'apple'], ['maker', 'motts'], ['price', 12] ], [ ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11] ] ] const data2 = data.map (pairs => pairs.reduce ((o, [k,v]) => ({ ...o, [k]: v }), {})) console.log (data2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.