1

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:

*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. 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')))
2
  • This question doesn't make a lot of sense. You say you want things grouped by family but they seemed to be grouped by manufacturer. You have a Switch manufacturer but you don't include the game in it. I suggest adding the exact output you want. Commented Nov 16, 2017 at 15:49
  • Sorry, wrong transcription. Replaced manufacturer with family Commented Nov 16, 2017 at 16:06

2 Answers 2

2

You could take a Map and group by family.

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; }

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry @Nina but if the family name is not found in the second one, at the moment you code fail...
what should happen in this case?
sorry, I updated the question. If the family of the array1 is not found in the array2, just create the family object with games: [] as you can see in the question. The array1 is the master. Thank you again
1

You can do this with reduce + filter.

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.