Home > database >  Merging two arrays of objects Javascript / Typescript
Merging two arrays of objects Javascript / Typescript

Time:07-19

I have two arrays of objects which looks something like this:

const users = [
    {
        status: 'failed',
        actionName: 'blabla',
        userId: 1,
    },
    {
        status: 'success',
        actionName: 'blablabla',
        userId: 2,
    },
];

Second one

const usersDetails = [
    {
        name: 'Joseph',
        id: 1,
    },
    {
        name: 'Andrew',
        id: 2,
    },
];

I want to check if userId is equal to id and if so then push the name from usersDetails into users objects. So output would look like this:

const users = [
{
status: 'failed',
actionName: 'blabla',
userId: 1,
name: 'Joseph'
},
{
status: 'success',
actionName: 'blablabla',
userId: 2,
name: 'Andrew'
}];

CodePudding user response:

The easiest solution would be to do:

const users = [
    {
        status: 'failed',
        actionName: 'blabla',
        userId: 1,
    },
    {
        status: 'success',
        actionName: 'blablabla',
        userId: 2,
    },
];

const usersDetails = [
    {
        name: 'Joseph',
        id: 1,
    },
    {
        name: 'Andrew',
        id: 2,
    },
];

const getAllUserInfo = () => users.map(user => {
  const userExtraInfo = usersDetails.find(details => details.id === user.userId)
  
  const fullUser = {...user, ...userExtraInfo}
  
  delete fullUser.id
  
  return fullUser
})

console.log(getAllUserInfo())

CodePudding user response:

const users = [
    {
        status: 'failed',
        actionName: 'blabla',
        userId: 1,
    },
    {
        status: 'success',
        actionName: 'blablabla',
        userId: 2,
    },
];

const usersDetails = [
    {
        name: 'Joseph',
        id: 1,
    },
    {
        name: 'Andrew',
        id: 2,
    },
];

users.forEach(each => {
    const found = usersDetails.find(detail => detail.id === each.userId);

    if (found) {
        each.name = found.name;
    }
});

console.log(users);

CodePudding user response:

You can try this code :

let result = users.map(user => ({...user, ...usersDetails.find(userDetail => userDetail.id == user.userId) }));
console.log(result);

If you only want to get name from the second array :

let result = users.map(user => ({...user, 'name': usersDetails.find(userDetail => userDetail.id == user.userId).name }));

If you want to get all properties exepted id ::

let result = users.map(user => {
    let result = {...user, ...usersDetails.find(userDetail => userDetail.id == user.userId) }
    delete result.id;
    return result;
});

CodePudding user response:

const users = [ { status: 'failed', actionName: 'blabla', userId: 1, }, { status: 'success', actionName: 'blablabla', userId: 2, }, ];
const usersDetails = [ { name: 'Joseph', id: 1, }, { name: 'Andrew', id: 2, }, ];

const newUsers = users.map(user => {
  user.name = usersDetails.find(u => u.id === user.userId)?.name;
  return user;
});

console.log(newUsers);

CodePudding user response:

Hope this answer will work for you

 const users = [
    {
      status: "failed",
      actionName: "blabla",
      userId: 1,
    },
    {
      status: "success",
      actionName: "blablabla",
      userId: 2,
    },
  ];
  
  const usersDetails = [
    {
      name: "Joseph",
      id: 1,
    },
    {
      name: "Andrew",
      id: 2,
    },
  ];
  
  users.map((e) => {
    usersDetails.find((_e) => {
      if (e.userId === _e.id) {
        e.name = _e.name;
      }
    });
  });
  console.log(users);

CodePudding user response:

you can do something like this using a single loop

const users = [
    {
      status: "failed",
      actionName: "blabla",
      userId: 1,
    },
    {
      status: "success",
      actionName: "blablabla",
      userId: 2,
    },
  ];
  
  const usersDetails = [
    {
      name: "Joseph",
      id: 1,
    },
    {
      name: "Andrew",
      id: 2,
    },
  ];
  
  const result = Object.values([...users, ...usersDetails].reduce((res, {userId, id,...item}) => {
    const key = id || userId
    return {
      ...res,
      [key]: {...(res[key] || {userId: key}), ...item}
    }
  }, {}))
  console.log(result);

  • Related