gpt4 book ai didi

javascript - 在不使用任何插件的情况下在 JavaScript 中将数组分组和映射为不同的格式

转载 作者:行者123 更新时间:2023-11-29 23:37:32 26 4
gpt4 key购买 nike

我有一个对象数组,如下所示,

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'
}]

基本上就是按年份分组,someProperty = true/false,但是希望这样格式化。我不能使用任何插件来执行此操作,可以使用 ES6。

我尝试通过编写一个按单个属性分组的函数来使用 reduce,但我不确定我们如何针对多个属性执行此操作并以这种方式进行投影。请在下面找到我要分组的代码以及我能够实现的目标,

let groupBy = function(arr, key) {
return arr.reduce(function(r, x) {
(r[x[key]] = r[x[key]] || []).push(x);
return r;
}, {});
};

我能够实现以下,

ableToDo = [{
'2010' : [
{id: 1, make: 'audi', year: '2010', someProperty: true},
{id: 4, make: 'vw', year: '2010', someProperty: true},
{id: 7, make: 'bmw', year: '2010', someProperty: false},
{id: 9, make: 'bmw', year: '2010', someProperty: true}
],
'2011' : [
{id: 2, make: 'bmw', year: '2011', someProperty: false},
{id: 3, make: 'bmw', year: '2011', someProperty: true},
{id: 5, make: 'vw', year: '2011', someProperty: true},
{id: 6, make: 'audi', year: '2011', someProperty: true},
{id: 8, make: 'bmw', year: '2011', someProperty: false}
]}]

最佳答案

您可以使用通过 reduce 积累的 ES6 Map 来完成此操作。这是它的样子:

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()];
}

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}
];

console.log(transform(cars));
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 在不使用任何插件的情况下在 JavaScript 中将数组分组和映射为不同的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46009755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com