我有一个以下格式的数组
cars = [
{id: 1, make: 'audi', year: '2010', someProperty: true},
{id: 2, make: 'bmw', year: '2011', someProperty: false},
{id: 3, make: 'bmw', year: '2011', someProperty: true},
{id: 4, make: 'vw', year: '2010', someProperty: true},
{id: 5, make: 'vw', year: '2011', someProperty: true},
{id: 6, make: 'audi', year: '2011', someProperty: true},
{id: 7, make: 'bmw', year: '2010', someProperty: false},
{id: 8, make: 'bmw', year: '2011', someProperty: false},
{id: 9, make: 'bmw', year: '2010', someProperty: true}
]
并将其转换为以下格式,
requiredFormat = [
{
somePropertyTrue: [
{id: 1, make: 'audi', year: '2010', someProperty: true},
{id: 4, make: 'vw', year: '2010', someProperty: true},
{id: 9, make: 'bmw', year: '2010', someProperty: true}
],
somePropertyFalse: [
{id: 7, make: 'bmw', year: '2010', someProperty: false}
],
year: '2010'
},
{
somePropertyTrue: [
{id: 3, make: 'bmw', year: '2011', someProperty: true},
{id: 5, make: 'vw', year: '2011', someProperty: true},
{id: 6, make: 'audi', year: '2011', someProperty: true}
],
somePropertyFalse: [
{id: 2, make: 'bmw', year: '2011', someProperty: false},
{id: 8, make: 'bmw', year: '2011', someProperty: false}
],
year: '2011'
}
]
我正在使用如下转换功能,
function transform(cars) {
return [...cars.reduce ( (acc, car) => {
const yearGrp = acc.get(car.year) || {
somePropertyTrue: [],
somePropertyFalse: [],
year: car.year
};
yearGrp['someProperty' + (car.someProperty ? 'True' : 'False')].push(car);
return acc.set(car.year, yearGrp);
}, new Map).values()];
}
我正在使用readline读取第一个数组并暂停每2000个项目,我正在尝试扩展上面的函数以接受先前转换的数组,以便将所有转换后的项目合并到一个数组中,我尝试了以下但我知道这不是解决这个问题的方法。
let transform = function transform(cars) {
return [...cars.reduce((acc, arr) => {
if (!first) {
parsedArray.reduce((a) => {
const yearGrp = a.get(arr.year) ||
acc.get(arr.year) || {
somePropertyTrue: [],
somePropertyFalse: [],
year: arr.year
};
yearGrp['someProperty' + (car.someProperty ? 'True' : 'False')].push(arr);
return a.set(arr.year, yearGrp);
});
} else {
const yearGrp = acc.get(arr.year) || {
somePropertyTrue: [],
somePropertyFalse: [],
year: arr.year
};
yearGrp['someProperty' + (car.someProperty ? 'True' : 'False')].push(arr);
return acc.set(arr.year, yearGrp);
}
}, new Map())];
};
这里,parsedArray是我第一次尝试填充并尝试每隔一段时间使用它,'first'是一个标志
答案 0 :(得分:0)
Array.prototype.reduce()
解决方案:
var cars = [
{id: 1, make: 'audi', year: '2010', someProperty: true}, {id: 2, make: 'bmw', year: '2011', someProperty: false},
{id: 3, make: 'bmw', year: '2011', someProperty: true}, {id: 4, make: 'vw', year: '2010', someProperty: true},
{id: 5, make: 'vw', year: '2011', someProperty: true}, {id: 6, make: 'audi', year: '2011', someProperty: true},
{id: 7, make: 'bmw', year: '2010', someProperty: false}, {id: 8, make: 'bmw', year: '2011', someProperty: false},
{id: 9, make: 'bmw', year: '2010', someProperty: true}
],
requiredFormatData = [];
// grouping by 'year'
groups = cars.reduce(function(r, o){
var k = 'someProperty' + ((o.someProperty)? 'True':'False');
if (r[o.year]){
(r[o.year][k]) && r[o.year][k].push(o) || (r[o.year][k] = [o]);
} else {
r[o.year] = {year: o.year};
r[o.year][k] = [o];
requiredFormatData.push(r[o.year]);
}
return r;
}, {});
console.log(requiredFormatData);

答案 1 :(得分:0)
最好保留地图/ id
以便进一步插入数据。如果需要转换后的数据,可以从中获取快照。
您可以在地图上使用闭包并返回函数,例如
company_name

acc