[英]How to group arrays of objects by value and add the result to another array with same value at specific key
I have this situation: 我有这种情况:
const array1 = [
{
node:
{
id: '789dg020hgn20ijahq',
family: 'ps4',
featuredImage: 'URL'
}
},
{
node:
{
id: 'gf80s70ga09ggds90ds000s9',
family: 'xbox-one',
featuredImage: 'URL'
}
}
]
const array2 = [
{
node:
{
id: 'h83g01whiehq8haos',
family: 'nintendo-switch',
gameName: 'Mario Cart'
}
},
{
node:
{
id: '1290qas9a0k19po1',
family: 'xbox-one',
gameName: 'COD WWII'
}
},
{
node:
{
id: '09ga09guh3njf32olkls',
family: 'xbox-one',
gameName: 'GOW 2'
}
}
]
What I am trying to achieve is to group these arrays by family
key and have this new array: 我想要实现的是按
family
密钥对这些数组进行分组并拥有这个新数组:
*output I want*
[{
family: {
id: 'gf80s70ga09ggds90ds000s9',
family: 'xbox-one',
featuredImage: 'URL',
games: [
0: {
id: '1290qas9a0k19po1',
family: 'xbox-one',
gameName: 'COD WWII'
},
1: {
id: '09ga09guh3njf32olkls',
family: 'xbox-one',
gameName: 'GOW 2'
}
]
},
family: {
id: 'h83g01whiehq8haos',
family: 'nintendo-switch',
featuredImage: 'URL',
games: []
}
]
*if the family of the array1 is not matched in array2*
family: {
id: '789dg020hgn20ijahq',
family: 'ps4',
featuredImage: 'URL',
games: []
}
Using this code, I grouped the arrays by family
, but I missed some informations. 使用这段代码,我按
family
对数组进行了分组,但我错过了一些信息。 How can I modify the following code to have the desired result? 如何修改以下代码以获得所需的结果? Sorry but I am a little bit stuck....
对不起,但我有点卡住....
const testing2 = result[1].map((obj) => { // result[1] comes from a Promise
return obj.node
})
_.mapValues(_.groupBy(testing2, 'family'), (list) => list.map((o) => _.omit(o, 'family')))
You could take a Map
and group by family
. 您可以按
family
拍摄Map
和分组。
var array1 = [{ node: { id: '789dg020hgn20ijahq', family: 'nintendo-switch', featuredImage: 'URL' } }, { node: { id: 'gf80s70ga09ggds90ds000s9', family: 'xbox-one', featuredImage: 'URL' } }], array2 = [{ node: { id: 'h83g01whiehq8haos', family: 'nintendo-switch', gameName: 'Mario Cart' } }, { node: { id: '1290qas9a0k19po1', family: 'xbox-one', gameName: 'COD WWII' } }, { node: { id: '09ga09guh3njf32olkls', family: 'xbox-one', gameName: 'GOW 2' } }, { node: { id: '09ga09guh3njf32olkls', family: 'foo', gameName: 'GOW 2' } }], map = new Map, result = array1.map(function (o) { map.set(o.node.family, []); return { family: Object.assign({}, o.node, { games: map.get(o.node.family) }) }; }); array2.forEach(o => { if (!map.has(o.node.family)) { map.set(o.node.family, []); result.push({ family: { family: o.node.family, games: map.get(o.node.family) } }); } map.get(o.node.family).push(o.node); }); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can do this with reduce
+ filter
. 您可以使用
reduce
+ filter
执行此操作。
const outputArr = array1.reduce((acc, nxt) => {
const {id, family, featuredImage} = nxt.node;
const newObj = {id, family, featuredImage, games: []};
const array2Match = array2.filter(obj => obj.node.family === family);
if(!!array2Match.length) {
newObj.games.push(array2Match[0]);
}
acc.push(newObj)
return acc;
}, []);
console.log(outputArr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.