首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将对象数组按值分组,并将结果添加到具有相同值的另一个数组中

如何将对象数组按值分组,并将结果添加到具有相同值的另一个数组中
EN

Stack Overflow用户
提问于 2017-11-16 15:31:56
回答 2查看 46关注 0票数 1

我有这样的情况:

代码语言:javascript
复制
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'
        }
      }
]

我试图实现的是按family键对这些数组进行分组,并拥有这个新数组:

代码语言:javascript
复制
*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: []
}

使用这段代码,我将数组按family分组,但遗漏了一些信息。如何修改以下代码以获得所需的结果?对不起,我有点卡住了.

代码语言:javascript
复制
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')))
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-16 16:19:50

您可以通过Mapfamily进行分组。

代码语言:javascript
复制
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);
代码语言:javascript
复制
.as-console-wrapper { max-height: 100% !important; top: 0; }

票数 2
EN

Stack Overflow用户

发布于 2017-11-19 20:29:43

您可以使用reduce + filter来完成这一任务。

演示

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47333518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档