我正在尝试寻找一种优雅而高效的方法来将对象数组转换为由键(在本例中为"groupByThisKey")索引的对象。一个hashmap,我相信它叫做。
我想出了一个原始版本
下面用一些数据进行了解释:
const arr = [
{
id: 1,
name: "one",
groupByThisKey: 'groupA'
},
{
id: 2,
name: "two",
groupByThisKey: 'groupB'
},
{
id: 3,
name: "three",
groupByThisKey: 'groupB'
},
{
id: 4,
name: "four",
groupByThisKey: 'groupA'
}
];
const groupedByKey = {};
arr.map(obj => {
if (groupedByKey[obj.groupByThisKey])
groupedByKey[obj.groupByThisKey].push(obj)
else
groupedByKey[obj.groupByThisKey] = [obj]
});
console.log(groupedByKey)
它确实给出了所需的输出:
{
groupA: [
{
id: 1,
name: "one",
groupByThisKey: 'groupA'
},
{
id: 4,
name: "four",
groupByThisKey: 'groupA'
}
],
groupB: [
{
id: 2,
name: "two",
groupByThisKey: 'groupB'
},
{
id: 3,
name: "three",
groupByThisKey: 'groupB'
}
]
}
但以一种相当原始的方式。我更愿意使用一种更短、更现代的方法来完成这项工作,如果可能的话,可以使用object.assign或reduce,但我不能理解它,因为我需要为每个键使用一个值数组。
我能够找到许多示例,这些示例似乎都适用于每个键只有一个值的用例,但在我的示例中,我需要一个由该键分组的对象的数组,所以我找到的示例只接受最新的值。下面是一个例子:
const hash = Object.assign({}, ...array.map(s => ({[s.key]: s.value})));
发布于 2019-04-23 03:02:48
你实现的东西是非常正确的,让我使用一个reducer来消除它的副作用:
const arr = [
{
id: 1,
name: "one",
groupByThisKey: 'groupA'
},
{
id: 2,
name: "two",
groupByThisKey: 'groupB'
},
{
id: 3,
name: "three",
groupByThisKey: 'groupB'
},
{
id: 4,
name: "four",
groupByThisKey: 'groupA'
}
];
console.log(arr.reduce((groupedByKey,obj) => {
if (groupedByKey[obj.groupByThisKey])
groupedByKey[obj.groupByThisKey].push(obj)
else
groupedByKey[obj.groupByThisKey] = [obj]
return groupedByKey;
}, {}));
这就是最fp式的编写方式。不是真正的性能,下面的下一个版本(之后)会有更好的性能,因为在每次迭代中创建对象或数组都不是新的。
const arr = [
{
id: 1,
name: "one",
groupByThisKey: 'groupA'
},
{
id: 2,
name: "two",
groupByThisKey: 'groupB'
},
{
id: 3,
name: "three",
groupByThisKey: 'groupB'
},
{
id: 4,
name: "four",
groupByThisKey: 'groupA'
}
];
console.log(arr.reduce(
(groups, x) =>
({
...groups,
[x.groupByThisKey]: [
...(groups[x.groupByThisKey] || []),
x
]
})
, {}
))
更简单的版本,与上面的逻辑相同:
const arr = [
{
id: 1,
name: "one",
groupByThisKey: 'groupA'
},
{
id: 2,
name: "two",
groupByThisKey: 'groupB'
},
{
id: 3,
name: "three",
groupByThisKey: 'groupB'
},
{
id: 4,
name: "four",
groupByThisKey: 'groupA'
}
];
console.log(arr.reduce(
(groups, x) => {
let key = x.groupByThisKey;
if (groups[key] === undefined)
groups[key] = [];
groups[key].push(x);
return groups;
}, {}
))
发布于 2019-04-23 02:59:21
现代的方式是使用Map
,而不是对象:
const groupedByKey = new Map();
for (const obj of arr) {
if (!groupedByKey.has(obj.groupByThisKey))
groupedByKey.set(obj.groupByThisKey, []);
groupedByKey.get(obj.groupByThisKey).push(obj);
}
https://stackoverflow.com/questions/55799551
复制相似问题