如何 union/group 对象数组的相同值和一些 属性?
How to union/group array of objects by same values and by some property?
我有一些数组:
[
{
billable: 1,
source: "Facebook",
providerAccountId: 5,
message_id: "5bb1a457"
},
{
billable: 1,
source: "Facebook",
providerAccountId: 5,
message_id: "6bb3G465"
},
{
destination: "8801856472841"
message_id: "5bb1a457"
}
]
//after union, i need to get result:
[
{
billable: 1,
source: "Facebook",
providerAccountId: 5,
message_id: "5bb1a457"
destination: "8801856472841"
},
{
billable: 1,
source: "Facebook",
providerAccountId: 5,
message_id: "6bb3G465"
}
]
请帮忙,myabe 是否可以通过具有唯一值的 属性 进行分组或联合。只是将所有具有相同值的对象通过指定的 peoperty 和缺少的属性添加到连接的对象中
您可以遍历数组并在应用已插入对象的属性时对其进行过滤。
该提议使用一个(真正的)空对象作为哈希表来引用被过滤的对象。如果存在具有相同 message_id
的对象,则将实际对象的所有属性分配给具有散列的对象。
var array = [{ billable: 1, source: "Facebook", providerAccountId: 5, message_id: "5bb1a457" }, { billable: 1, source: "Facebook", providerAccountId: 5, message_id: "6bb3G465" }, { destination: "8801856472841", message_id: "5bb1a457" }];
array = array.filter(function (a) {
if (!this[a.message_id]) {
this[a.message_id] = a;
return true;
}
Object.keys(a).forEach(function (k) {
this[k] = a[k];
}, this[a.message_id]);
}, Object.create(null));
console.log(array);
您可以通过您要检查的键映射它并最终合并它:
var d = [
{
billable: 1,
source: "Facebook",
providerAccountId: 5,
message_id: "5bb1a457"
},
{
billable: 1,
source: "Facebook",
providerAccountId: 5,
message_id: "6bb3G465"
},
{
destination: "8801856472841",
message_id: "5bb1a457"
}
];
var res = d.reduce((ac,x) => {
if (ac[x.message_id])
Object.assign(ac[x.message_id],x); //if you want to preserve original use a new object as target
else
ac[x.message_id]=x;
return ac;
},{})
// and then you can map it back to array
var res2 = Object.keys(res).map(x => res[x])
console.log(res)
console.log(res2)
我有一些数组:
[
{
billable: 1,
source: "Facebook",
providerAccountId: 5,
message_id: "5bb1a457"
},
{
billable: 1,
source: "Facebook",
providerAccountId: 5,
message_id: "6bb3G465"
},
{
destination: "8801856472841"
message_id: "5bb1a457"
}
]
//after union, i need to get result:
[
{
billable: 1,
source: "Facebook",
providerAccountId: 5,
message_id: "5bb1a457"
destination: "8801856472841"
},
{
billable: 1,
source: "Facebook",
providerAccountId: 5,
message_id: "6bb3G465"
}
]
请帮忙,myabe 是否可以通过具有唯一值的 属性 进行分组或联合。只是将所有具有相同值的对象通过指定的 peoperty 和缺少的属性添加到连接的对象中
您可以遍历数组并在应用已插入对象的属性时对其进行过滤。
该提议使用一个(真正的)空对象作为哈希表来引用被过滤的对象。如果存在具有相同 message_id
的对象,则将实际对象的所有属性分配给具有散列的对象。
var array = [{ billable: 1, source: "Facebook", providerAccountId: 5, message_id: "5bb1a457" }, { billable: 1, source: "Facebook", providerAccountId: 5, message_id: "6bb3G465" }, { destination: "8801856472841", message_id: "5bb1a457" }];
array = array.filter(function (a) {
if (!this[a.message_id]) {
this[a.message_id] = a;
return true;
}
Object.keys(a).forEach(function (k) {
this[k] = a[k];
}, this[a.message_id]);
}, Object.create(null));
console.log(array);
您可以通过您要检查的键映射它并最终合并它:
var d = [
{
billable: 1,
source: "Facebook",
providerAccountId: 5,
message_id: "5bb1a457"
},
{
billable: 1,
source: "Facebook",
providerAccountId: 5,
message_id: "6bb3G465"
},
{
destination: "8801856472841",
message_id: "5bb1a457"
}
];
var res = d.reduce((ac,x) => {
if (ac[x.message_id])
Object.assign(ac[x.message_id],x); //if you want to preserve original use a new object as target
else
ac[x.message_id]=x;
return ac;
},{})
// and then you can map it back to array
var res2 = Object.keys(res).map(x => res[x])
console.log(res)
console.log(res2)