假设我有这个javascript数组。
var events = [
{id: 1, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 2, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 2, name: 'Football Game', invitees: ['Gary', 'Jane','Bella']},
{id: 3, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 4, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 5, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 5, name: 'Football Game', invitees: ['Ted', 'Laura']},
{id: 5, name: 'Football Game', invitees: ['Jim', 'Samantha']},
];
我需要的是将此数组减少为:
var events = [
{id: 1, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 2, name: 'Football Game', invitees: ['Gary', 'Jane','Bella']},
{id: 3, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 4, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 5, name: 'Football Game', invitees: ['Gary', 'Jane', 'Ted', 'Laura', 'Jim', 'Samantha']}
];
在减少数组的情况下,每个id只有一个对象,每个对象中的被邀请者数组显示所有被邀请者。
我在这里开始使用基本逻辑时遇到了麻烦。
答案 0 :(得分:1)
您可以使用reduce()
和forEach()
这样做。
var events = [
{id: 1, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 2, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 2, name: 'Football Game', invitees: ['Gary', 'Jane','Bella']},
{id: 3, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 4, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 5, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 5, name: 'Football Game', invitees: ['Ted', 'Gary', 'Laura']},
{id: 5, name: 'Football Game', invitees: ['Jim', 'Samantha']}
];
var o = {}
var result = events.reduce(function(r, e) {
if (!o[e.id]) {
o[e.id] = e;
r.push(o[e.id]);
} else {
e.invitees.forEach(function(a) {
if (o[e.id].invitees.indexOf(a) == -1) o[e.id].invitees.push(a);
})
}
return r;
}, [])
console.log(result)
您也可以使用Set
代替forEach()
循环删除重复项。
var events = [
{id: 1, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 2, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 2, name: 'Football Game', invitees: ['Gary', 'Jane','Bella']},
{id: 3, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 4, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 5, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 5, name: 'Football Game', invitees: ['Ted', 'Gary', 'Laura']},
{id: 5, name: 'Football Game', invitees: ['Jim', 'Samantha']}
];
var o = {}
var result = events.reduce(function(r, e) {
if (!o[e.id]) {
o[e.id] = e;
r.push(o[e.id]);
} else {
o[e.id].invitees = [...new Set(o[e.id].invitees.concat(e.invitees))];
}
return r;
}, [])
console.log(result)
答案 1 :(得分:0)
您可以使用此ES6 函数式编程样式代码:
var result = Array.from (
events.reduce ( (acc, obj) => acc.set(obj.id,
Object.assign({}, obj, {invitees: [...new Set(
[...(acc.get(obj.id) || obj).invitees, ...obj.invitees] )]})
), new Map() ), ([id, obj]) => obj );
var events = [
{id: 1, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 2, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 2, name: 'Football Game', invitees: ['Gary', 'Jane','Bella']},
{id: 3, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 4, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 5, name: 'Football Game', invitees: ['Gary', 'Jane']},
{id: 5, name: 'Football Game', invitees: ['Ted', 'Laura']},
{id: 5, name: 'Football Game', invitees: ['Jim', 'Samantha']},
];
var result = Array.from (
events.reduce ( (acc, obj) => acc.set(obj.id,
Object.assign({}, obj, {invitees: [...new Set(
[...(acc.get(obj.id) || obj).invitees, ...obj.invitees] )]})
), new Map() ), ([id, obj]) => obj );
console.log(result);