score:0

You could use 'array.concat([])' to merge two array objects together. See bellow example.

let UserId = [{ id: 1, name: "john" }, { id: 2, name: "micheal" }];
const userCredentials = [{ id: 1, country: "de" },{ id: 1, country: "us" }];

const newArray = UserId.concat(userCredentials);

Since you have defined UserId as a const you cannot change it. So you have to make it to let or var to modify the variable.

score:4

Basically you need to map thru 1 array and find if each object in the array exists in another array and use spread operator and return the merged object in map callback

Working demo

Use the code below:

    // option 1 - if you know the keys of the object
    let merged = UserId.map(u => {
      const user = userCredentials.find(uc => uc.id === u.id);
      if (user) u["country"] = user.country;
      return u;
    });

    // option 2 - generic merge
    let merged2 = UserId.map(u => {
      const user = userCredentials.find(uc => uc.id === u.id);
      if (user) return { ...u, ...user };
      return u;
    });

Related Query

More Query from same tag