Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
Merge 2 arrays and created a new one

I have 2 arrays this

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

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

See, I have some common records in both arrays and one is missing in array2. I want to merge them into one if employeeId doesn't match. Like this.

let array3 = [
   {employeeId: 1997, isActive: false}, 
   {employeeId: 199, isActive: true}, 
   {employeeId: 19, isActive: true}, 
   {employeeId: 191, isActive: true},
   {employeeId: 1221, isActive: false}
]

array3[3].isActive is false because it doesn't match in both arrays.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

  1. Get an array of employeeId from array1
  2. Get an array of employeeId from array2
  3. Create an array with all the (unique) emplyeeId's
  4. Create an array with the ID's that are not matching
  5. map() over all the ids (array_0_ids) to:
    1. Create an object
    2. Set isActive to the result of !non_matching.includes(emplyeeId)

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

let array_1_ids = array1.map(i => i.employeeId);
let array_2_ids = array2.map(i => i.employeeId);
let array_0_ids = [ ...array_1_ids, ...array_2_ids ].filter((v, i, s) => s.indexOf(v) === i);

let non_matching = array_2_ids.filter(id => !array_1_ids.includes(id));

let result = array_0_ids.map(emplyeeId => {
  return {
    emplyeeId,
    isActive: !non_matching.includes(emplyeeId)
  };
});

console.log(result);

[
  {"emplyeeId": 199, "isActive": true },
  {"emplyeeId": 19, "isActive": true}, 
  {"emplyeeId": 191, "isActive": true},
  {"emplyeeId": 1221, "isActive": false} 
]
about 4 years ago · Juan Pablo Isaza Report

0

let result = [];

const employeeIdArray2 = array2.map(e => e.employeeId);
const employeeIdArray1 = array1.map(e => e.employeeId);

const missingEmployeeIdArray = employeeIdArray2.length > employeeIdArray1.length ? employeeIdArray1 : employeeIdArray2;

result = array2.map(e => {
  if (missingEmployeeIdArray.includes(e.employeeId)) {
    return {
      ...e,
      isActive: true
    }
  } else {
    return {
      ...e,
      isActive: false
    }
  }
})
about 4 years ago · Juan Pablo Isaza Report

0

  1. Create an object where the key will be the employeeId and the value will be the number of occurrences
  2. Create the final array by iterating over the object entries, set isActive to true when the number of occurrences is 2.

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

const array2 = [
  {
    designation: "SSE",
    emailId: "abc@gmail.com",
    employeeId: 199,
    firstName: "user2",
  },
  {
    designation: "DEVELOPER",
    emailId: "ab@gmail.com",
    employeeId: 19,
    firstName: "user1",
  },
  {
    designation: "DEVELOPER",
    emailId: "ab@gmail.com",
    employeeId: 191,
    firstName: "user1",
  },
  {
    designation: "TESTER",
    emailId: "ab@gmail.com",
    employeeId: 1221,
    firstName: "user1",
  },
];

const reducer = (acc, e) => ({
  ...acc,
  [e.employeeId]: (acc[e.employeeId] + 1) || 1,
}); 

const arr3 = Object.entries([...array1, ...array2].reduce(reducer, {}))
  .map(([employeeId, count]) => ({ employeeId, isActive: count == 2 }));
  
console.log(arr3);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!