1

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'
}];
1
  • Well thanks for all your answers. Im not sure if i should post another question, but what would be a solution if both arrays are Observables? Commented Jul 19, 2022 at 8:50

6 Answers 6

1

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

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

Comments

1

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

Comments

0

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

Comments

0

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

Comments

0

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

Comments

0

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

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.