Home > Blockchain >  Create one array from 2 diffrent array object and add key
Create one array from 2 diffrent array object and add key

Time:12-29

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

let array1 = [
    {
        designation: "SSE",
        emailId: "[email protected]",
        employeeId: 199,
        firstName: "user2",
    },
    {
        designation: "DEVELOPER",
        emailId: "[email protected]",
        employeeId: 19,
        firstName: "user1",
    },
];

let array2 = [
    {
        designation: "SSE",
        emailId: "[email protected]",
        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 });
        }
    });
});

CodePudding user response:

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}}))
];
*/

CodePudding user response:

Use map and spread

    let array1 = [{designation: "SSE", emailId: "[email protected]", employeeId: 199,firstName: "user2"}, {designation: "DEVELOPER", emailId: "[email protected]", employeeId: 19,firstName: "user1"}];
    
    let array2 = [{designation: "SSE", emailId: "[email protected]", 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.

CodePudding user response:

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

let array1 = [
  {
    designation: "SSE",
    emailId: "[email protected]",
    employeeId: 199,
    firstName: "user2"
  },
  {
    designation: "DEVELOPER",
    emailId: "[email protected]",
    employeeId: 19,
    firstName: "user1"
  }
];

let array2 = [
  {
    designation: "SSE",
    emailId: "[email protected]",
    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);
  • Related