简体   繁体   English

如何按值对对象数组进行分组,并将结果添加到另一个在特定键上具有相同值的数组中

[英]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执行此操作。

DEMO DEMO

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.

相关问题 如何通过另一个对象数组过滤对象数组并将键/值添加到结果数组? - How to filter array of objects by another array of objects and add key/value to the result array? 如果特定键的值相同,如何将值更新为嵌套的对象数组? - How to update the value into a nested array of objects if value for specific key is same? 使用Lodash或Underscore JS,如何基于同一对象数组中的另一个键值获取特定的键值 - Using Lodash or Underscore JS , how do I obtain a specific key value based on another key value within the same objects array React:过滤从具有特定键的对象数组中获取值并将结果分组到数组中 - React : filter get value from array of objects with specific key and group result into array 如何检查对象数组是否包含特定键的相同值 - How to check if the array of objects contains same value for a specific key 如何检查我推入对象的对象数组是否具有来自另一个对象数组的特定键值 - How to check if an array of objects where I push into objects has a specific key value from another array of objects 如何按值对同一项目的对象数组进行分组? - How to group an array of objects by value for the same item? 如何在javascript中的对象数组中分组和添加它们的值? - How to group and add their value in array of objects in javascript? 具有特定键值的对象组数组首先推送 - Group array of objects with a specific key value pushed first 如何将键值对添加到数组中的对象? - How to add key value pair to objects in an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM