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

105
Views
How to merge two object arrays by adding matching object in as a new field

I have these two arrays:

const goodCars = [
    { name: 'ferrari', price: 22000, condition: { isGood: true, isBad: false } },
    { name: 'ford', price: 21000, condition: { isGood: true, isBad: false } },
    { name: 'bmw', price: 20000, condition: { isGood: true, isBad: false } },
  ];

  const badCars = [
    { name: 'ford', price: 1111, condition: { isGood: false, isBad: true } },
    { name: 'honda', price: 8000, condition: { isGood: false, isBad: true } },
  ];

My goal is to produce this final array:

  const finalCarList = [
    { name: 'ferrari', price: 22000, condition: { isGood: true, isBad: false } },
    {
      name: 'ford',
      price: 21000,
      condition: { isGood: true, isBad: false },
      toCompareWith: { name: 'ford', price: 1111, condition: { isGood: false, isBad: true } },
    },
    { name: 'bmw', price: 20000, condition: { isGood: true, isBad: false } },
    { name: 'honda', price: 8000, condition: { isGood: false, isBad: true } },
  ];

Basically I want to merge the two goodCars and badCars arrays into one but if the cars exists in both good and badd arrays then I want to add the bad car to the good car array as a new field toCompareWith: {...} (seen above)

I've tried using map(), reduce(), for loops, etc. but my brain has hit a wall and I'm not getting any closer.

My attempt:

 goodCars.map((gc) => {
    badCars.map((bc) => {
      if (isCarMatch(gc, bc)) {
        finalCarList = [
          ...finalCarList,
          { ...gc, toCompareWith: bc },
        ];
      }
    });
 });

Answer I went with based on the below marked correct one: ✅

  let finalCarList: Array<Car> = [...goodCars];

  badCars?.forEach((bc) => {
    const match: Car | undefined = goodCars.find((gc) => isCarMatch(gc, bc));
    match
      ? (match.toCompareWith = bc) // Slot in matching item
      : (finalCarList = [...finalCarList, bc]);
  });
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You shouldn't use nested loops.

Start by copying goodCars to finalCarList. Then loop over badCars. If the car is in goodCars, add the bad car as the toCompareWith property. Otherwise, push it into finalCarList.

finalCarList = [...goodCars];
badCars.forEach(bc => {
    match = goodCars.find(gc => isCarMatch(gc, bc));
    if (match) {
        match.toCompareWith = bc;
    } else {
        finalCarList.push(bc);
    }
});

Also, in general you shouldn't use map() if the callback function doesn't return anything. If it's called only for side effects, use forEach().

about 4 years ago · Juan Pablo Isaza Report

0

Below is an example using two for...of loops. The outer loop is the first array and the inner loop is the second array using .entries() in order to track a matches index. If there's a match, the object in the second array is removed and is assigned as the value of .toCompareWith property of the matched object of the first array. After the loops are completed, both arrays are merged.

const goodCars = [
    { name: 'ferrari', price: 22000, condition: { isGood: true, isBad: false } },
    { name: 'ford', price: 21000, condition: { isGood: true, isBad: false } },
    { name: 'bmw', price: 20000, condition: { isGood: true, isBad: false } },
  ];

  const badCars = [
    { name: 'ford', price: 1111, condition: { isGood: false, isBad: true } },
    { name: 'honda', price: 8000, condition: { isGood: false, isBad: true } },
  ];
  
const mergeCars = (arrA, arrB) => {
  for (let carA of arrA) {
    for (let [idx, carB] of arrB.entries()) {
      if (carA.name === carB.name && carA.condition.isGood !== carB.condition.isGood) {
        carA.toCompareWith = carB;
        arrB.splice(idx, 1);
      }
    }
  }
  return [...arrA, ...arrB];
};

console.log(mergeCars(goodCars, badCars));

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!