Home > Software design >  What is it a good way in javascript to merge two array of objects when there is a match with a speci
What is it a good way in javascript to merge two array of objects when there is a match with a speci

Time:11-15

In a project I'm working on, I need to merge 2 arrays of objects and as a result one new array of objects containing the merged key/values where two Ids match.

To give an example I created the following snippet but is not fully correct on what I want to achieve. More details after the snippet

const users = [{
    id: 'Ae7uWu7LjwoEgVqzFU5xc',
    firstName: 'Carl',
    lastName: 'Jones'
  },
  {
    id: 't2wzj8dh4r-qw1_SW-IOE',
    firstName: 'Chloe',
    lastName: 'Kearney'
  },
  {
    id: '50Zwvw37OejbQBG7csZWJ',
    firstName: 'Gemma',
    lastName: 'Sloan'
  },
  {
    id: 'NpcXdEKqfzhVCZOJ1dKuw',
    firstName: 'Dario',
    lastName: 'Angelini'
  },
  {
    id: 'e95ZG9IfV442HdJp-CaBL',
    firstName: 'Mindy',
    lastName: 'Schreiber'
  },
  {
    id: 'eAMv8AbynYRkdBPE5Scm2',
    firstName: 'Xdax',
    lastName: 'Rufq'
  },
  {
    id: 'egMXnFvoMM7f4in3Se4Ui',
    firstName: 'Mtajx',
    lastName: 'Plde'
  },
  {
    id: '6kbPT-HC5-szACuJ85I6r',
    firstName: 'Zsofi',
    lastName: 'Toke'
  }
]

const comments = [{
    content: 'Patient follow up call scheduled for 11Nov2021 at 8am',
    stage: 'AT_CALLCENTER',
    userId: 't2wzj8dh4r-qw1_SW-IOE',
    createdAt: '2021-10-29T11:41:11.780Z'
  },
  {
    content: 'Patient confirmed GP referral on [date]',
    stage: 'AT_CALLCENTER',
    userId: 't2wzj8dh4r-qw1_SW-IOE',
    createdAt: '2021-10-29T11:41:42.237Z'
  },
  {
    content: 'Candidate called on [date] and visit scheduled for [date] ',
    stage: 'AT_SITE',
    userId: 't2wzj8dh4r-qw1_SW-IOE',
    createdAt: '2021-10-29T11:43:42.140Z'
  },
  {
    content: 'Candidate test result was positive for Pompe disease on [date]',
    stage: 'AT_SITE',
    userId: 't2wzj8dh4r-qw1_SW-IOE',
    createdAt: '2021-10-29T11:45:21.716Z'
  }
]

const result = users.map(t1 => ({
  ...t1,
  content: comments.filter(t2 => t2.userId === t1.id).map(t1 => t1.content),
  created: comments.filter(t2 => t2.userId === t1.id).map(t1 => t1.createdAt),
}));

console.log(result)

The result I was able to get from that try was an array of objects but the comments are represented as an array inside the user object itself as for the createdAt

this is an example of what I'm getting now

 [{
    "id": "t2wzj8dh4r-qw1_SW-IOE",
    "firstName": "Chloe",
    "lastName": "Kearney",
    "content": [
      "Patient follow up call scheduled for 11Nov2021 at 8am",
      "Patient confirmed GP referral on [date]",
      "Candidate called on [date] and visit scheduled for [date] ",
      "Candidate test result was positive for Pompe disease on [date]"
    ],
    "created": [
      "2021-10-29T11:41:11.780Z",
      "2021-10-29T11:41:42.237Z",
      "2021-10-29T11:43:42.140Z",
      "2021-10-29T11:45:21.716Z"
    ]
  },]

What I would like to get is the way around, I mean I want the comments to include the firstName and lastName of the user when the comments.userId is equal to the users.id

Example of the goal I want to achieve

consider the 2 objects below

comments = [{
    content: 'Patient follow up call scheduled for 11Nov2021 at 8am',
    stage: 'AT_CALLCENTER',
    userId: 't2wzj8dh4r-qw1_SW-IOE',
    createdAt: '2021-10-29T11:41:11.780Z'
  },
  {
    content: 'Patient confirmed GP referral on [date]',
    stage: 'AT_CALLCENTER',
    userId: 't2wzj8dh4r-qw1_SW-IOE',
    createdAt: '2021-10-29T11:41:42.237Z'
  }, { ...some other comments...}]

users = [
  {
    id: 't2wzj8dh4r-qw1_SW-IOE',
    firstName: 'Chloe',
    lastName: 'Kearney'
  },
{ ...some other users... }]

The result I'm seeking after the 2 objects are merged is as follows

res = [
      { 
        firstName: 'Chloe',
        lastName: 'Kearney',
        content: 'Patient follow up call scheduled for 11Nov2021 at 8am',
        stage: 'AT_CALLCENTER',
        userId: 't2wzj8dh4r-qw1_SW-IOE',
        createdAt: '2021-10-29T11:41:11.780Z'
     },
      {
        firstName: 'Chloe',
        lastName: 'Kearney',
        content: 'Patient confirmed GP referral on [date]',
        stage: 'AT_CALLCENTER',
        userId: 't2wzj8dh4r-qw1_SW-IOE',
        createdAt: '2021-10-29T11:41:42.237Z'
      }
     ]

As above I'm adding the first and last name of the user with the comments where there is an Ids match, so they are together after the merging.

I'm looking to understand a good way of doing this

CodePudding user response:

const comments = [{
  content: 'Patient follow up call scheduled for 11Nov2021 at 8am',
  stage: 'AT_CALLCENTER',
  userId: 't2wzj8dh4r-qw1_SW-IOE',
  createdAt: '2021-10-29T11:41:11.780Z'
},{
  content: 'Patient confirmed GP referral on [date]',
  stage: 'AT_CALLCENTER',
  userId: 't2wzj8dh4r-qw1_SW-IOE',
  createdAt: '2021-10-29T11:41:42.237Z'
}];

const users = [{
  id: 't2wzj8dh4r-qw1_SW-IOE',
  firstName: 'Chloe',
  lastName: 'Kearney'
}];

console.log(
  comments.map(comment => {
    const {id, ...user} = users.find(({id}) => id===comment.userId);
    return ({...user, ...comment});
}));

  • Related