在es6中重组groupby对象数组
restructuring groupby array of objects in es6
这是我有的数据响应,需要将其格式化为预期的:基于 rowindex 需要格式化 objects.better 解决方案的数组。
输入:
productdata:[ { rowIndex:1 , columnKey:'productName'},
{ rowIndex:1 , columnKey:'productID'},
{ rowIndex:1 , columnKey:'productDate'},
{ rowIndex:2 , columnKey:'productName'},
{ rowIndex:2 , columnKey:'productID'},
{ rowIndex:2 , columnKey:'productDate'},
],
预计O/p
产品数据:
[ { rowIndex:1 , columnKey:'productName'},
{ rowIndex:1 , columnKey:'productID'},
{ rowIndex:1 , columnKey:'productDate'}],
[{ rowIndex:2 , columnKey:'productName'},
{ rowIndex:2 , columnKey:'productID'},
{ rowIndex:2 , columnKey:'productDate'}],etc
您可以使用Array.prototype.reduce
创建数组
const productData = [
{ rowIndex:1 , columnKey:'productName'},
{ rowIndex:1 , columnKey:'productID'},
{ rowIndex:1 , columnKey:'productDate'},
{ rowIndex:2 , columnKey:'productName'},
{ rowIndex:2 , columnKey:'productID'},
{ rowIndex:2 , columnKey:'productDate'},
];
const result = productData.reduce((acc, curr) => {
const arrIndex = curr.rowIndex - 1;
if (acc[arrIndex]) acc[arrIndex].push(curr);
else acc[arrIndex] = [curr];
return acc;
}, []);
console.log(result);
这种方法如何使用 reduce
函数:
var productdata=[ { rowIndex:1 , columnKey:'productName'}, { rowIndex:1 , columnKey:'productID'}, { rowIndex:1 , columnKey:'productDate'}, { rowIndex:2 , columnKey:'productName'}, { rowIndex:2 , columnKey:'productID'}, { rowIndex:2 , columnKey:'productDate'}];
var result = Object.values(productdata.reduce((acc, elem)=>{
acc[elem.rowIndex] = acc[elem.rowIndex] || [];
acc[elem.rowIndex].push(elem);
return acc;
},{}));
console.log(result);
这是我有的数据响应,需要将其格式化为预期的:基于 rowindex 需要格式化 objects.better 解决方案的数组。
输入:
productdata:[ { rowIndex:1 , columnKey:'productName'},
{ rowIndex:1 , columnKey:'productID'},
{ rowIndex:1 , columnKey:'productDate'},
{ rowIndex:2 , columnKey:'productName'},
{ rowIndex:2 , columnKey:'productID'},
{ rowIndex:2 , columnKey:'productDate'},
],
预计O/p 产品数据:
[ { rowIndex:1 , columnKey:'productName'},
{ rowIndex:1 , columnKey:'productID'},
{ rowIndex:1 , columnKey:'productDate'}],
[{ rowIndex:2 , columnKey:'productName'},
{ rowIndex:2 , columnKey:'productID'},
{ rowIndex:2 , columnKey:'productDate'}],etc
您可以使用Array.prototype.reduce
创建数组
const productData = [
{ rowIndex:1 , columnKey:'productName'},
{ rowIndex:1 , columnKey:'productID'},
{ rowIndex:1 , columnKey:'productDate'},
{ rowIndex:2 , columnKey:'productName'},
{ rowIndex:2 , columnKey:'productID'},
{ rowIndex:2 , columnKey:'productDate'},
];
const result = productData.reduce((acc, curr) => {
const arrIndex = curr.rowIndex - 1;
if (acc[arrIndex]) acc[arrIndex].push(curr);
else acc[arrIndex] = [curr];
return acc;
}, []);
console.log(result);
这种方法如何使用 reduce
函数:
var productdata=[ { rowIndex:1 , columnKey:'productName'}, { rowIndex:1 , columnKey:'productID'}, { rowIndex:1 , columnKey:'productDate'}, { rowIndex:2 , columnKey:'productName'}, { rowIndex:2 , columnKey:'productID'}, { rowIndex:2 , columnKey:'productDate'}];
var result = Object.values(productdata.reduce((acc, elem)=>{
acc[elem.rowIndex] = acc[elem.rowIndex] || [];
acc[elem.rowIndex].push(elem);
return acc;
},{}));
console.log(result);