-3

I want to merge 2 array objects into one like this.

let array1 = [
    {
        designation: "SSE",
        emailId: "abc@gmail.com",
        employeeId: 199,
        firstName: "user2",
    },
    {
        designation: "DEVELOPER",
        emailId: "ab@gmail.com",
        employeeId: 19,
        firstName: "user1",
    },
];

let array2 = [
    {
        designation: "SSE",
        emailId: "abc@gmail.com",
        employeeId: 77,
        firstName: "user2",
    },
];

What's the best way performance-wise to generate an array of objects with a similar structure to this:

let array3 = [
    { employeeId: 199, isActive: true },
    { employeeId: 19, isActive: true },
    { employeeId: 77, isActive: false },
];

So basically, array1 is active users and array2 is inactive users.

What I have tried:

let item = [];
let filteredArray = array1.map(item => {
    array2.map(item2 => {
        if (item.employeeId === item2.employeeId) {
            item.push({ isActve: true, employeeId: item.employeeId });
        } else {
            item.push({ isActve: false, employeeId: item.employeeId });
        }
    });
});
ikhvjs
  • 5,316
  • 2
  • 13
  • 36
Gajen
  • 456
  • 1
  • 5
  • 17
  • Does this answer your question? [How to merge two arrays in JavaScript and de-duplicate items](https://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items) – Martin Dec 29 '21 at 12:33
  • @charlietfl, I have tried this let item = []; let filteredArray = array1.map(item => { array2.map(item2 => { if(item.employeeId === item2.employeeId){ item.push({isActve: true, employeeId: item.employeeId:}) }else { item.push({isActve: false, employeeId: item.employeeId:}) } }) }) – Gajen Dec 29 '21 at 12:34
  • [edit] the question so that code can be easily seen by all in formatted view. Blobs of code in comment blocks are not practical – charlietfl Dec 29 '21 at 12:35
  • You are using `map()` simply to loop over arrays. There are other looping methods to do that. `map()` is used to return a new array from an existing one. Then the id matching doesn't seem to make sense. You stated that each array contains different active status. So same employee shouldn't exist in both correct? – charlietfl Dec 29 '21 at 12:40

3 Answers3

2
let array3 = [
...array1.map(a => {return {employeeId: a.employeeId, isActive: true}}),
...array2.map(a => {return {employeeId: a.employeeId, isActive: false}})
];

/*
Object destructuring..

let array3 = [
...array1.map(({employeeId}) => ({employeeId, ...{isActive: true}})),
...array2.map(({employeeId}) => ({employeeId, ...{isActive: false}}))
];
*/
Julius
  • 491
  • 2
  • 10
  • thanks a ton. Love the approach. Can you look into this question? https://stackoverflow.com/questions/70519969/merge-2-arrays-and-created-a-new-one – Gajen Dec 29 '21 at 13:17
0

Use map and spread

    let array1 = [{designation: "SSE", emailId: "abc@gmail.com", employeeId: 199,firstName: "user2"}, {designation: "DEVELOPER", emailId: "ab@gmail.com", employeeId: 19,firstName: "user1"}];
    
    let array2 = [{designation: "SSE", emailId: "abc@gmail.com", employeeId: 77,firstName: "user2"}];
    
    const active = array1.map(obj => ({
      employeeId: obj.employeeId,
      isActive: true
    }))
    
    const inactive = array2.map(obj => ({
      employeeId: obj.employeeId,
      isActive: false
    }))
    
    const combine = [
    ...active,
    ...inactive
    ]


console.log(combine)

What we're doing here is mapping the first array with the information we need (employeeId) and setting the isActive status.

We do the same thing for the second array but set isActive = false.

Then we combine the two arrays with the spread operator.

Bergur
  • 3,962
  • 12
  • 20
0

here, just you have to iterate in 2 array as per below code

let array1 = [
  {
    designation: "SSE",
    emailId: "abc@gmail.com",
    employeeId: 199,
    firstName: "user2"
  },
  {
    designation: "DEVELOPER",
    emailId: "ab@gmail.com",
    employeeId: 19,
    firstName: "user1"
  }
];

let array2 = [
  {
    designation: "SSE",
    emailId: "abc@gmail.com",
    employeeId: 77,
    firstName: "user2"
  }
];

let newArr = [];
array1.forEach(item => {
  newArr = [...newArr, { employeeId: item.employeeId, isActive: true }];
});
array2.forEach(item => {
  newArr = [...newArr, { employeeId: item.employeeId, isActive: false }];
});
console.log(newArr);