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

61
Views
Combine same year items to one object

I have an array of objects that look like this:

[
  {
    makeYear: 2019,
    company: 'Mazda',
    fleet: '900'
  },
  {
    makeYear: 2019,
    company: 'Mercedes',
    fleet: '500'
  },
  {
    makeYear: 2020,
    company: 'Honda',
    fleet: '390'
  },
  .....
]

I am trying to merge all the properties for a specific makeYear into one object that should look like this

[
  {
    makeYear: 2019,
    Mazda: 900,
    Mercedes: 500,
    //other companies present for the year
  },
  {
    makeYear: 2020,
    Honda: 390,
    //other companies present for the year
  },
  
];

I have tried it doing this way, but no luck.

const mergeObjs = input.reduce((res, item) => {
  if (!item.makeYear) return res;
  res[item.makeYear] = item;
  return res;
}, {});

const merged = input.filter(item => item.makeYear).map(item => {
  return {...item, ...(mergeObjs[item.makeYear] || {})}
});

Any idea how I can achieve this? TIA

almost 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use reduce to group with makeYear as a key.

const data = [
  {
    makeYear: 2019,
    company: 'Mazda',
    fleet: '900'
  },
  {
    makeYear: 2019,
    company: 'Mercedes',
    fleet: '500'
  },
  {
    makeYear: 2020,
    company: 'Honda',
    fleet: '390'
  },
]


const result = data.reduce((acc, {makeYear, company, fleet}) => {
  let item = acc.find(i => i.makeYear === makeYear);
  if (item) {
    item[company] = fleet;
  } else {
    acc.push({
      makeYear,
      [company]: fleet,
    })
  }
  return acc
}, [])

console.log(result)

almost 4 years ago · Santiago Trujillo Report

0

You can use reduce() to collect values for each year. Then use entries() and map() to get your desired structure.

This implementation uses a Map, but you can also use a plain JavaScript object.

const input = [
  {
    makeYear: 2019,
    company: "Mazda",
    fleet: "900",
  },
  {
    makeYear: 2019,
    company: "Mercedes",
    fleet: "500",
  },
  {
    makeYear: 2020,
    company: "Honda",
    fleet: "390",
  },
];

const carsByYearAndMake = input.reduce((carsByYear, carsByMake) => {
  if (carsByYear.has(carsByMake.makeYear)) {
    const carsForYear = carsByYear.get(carsByMake.makeYear);
    carsForYear[carsByMake.company] = carsByMake.fleet;
  } else {
    carsByYear.set(carsByMake.makeYear, {
      [carsByMake.company]: carsByMake.fleet,
    });
  }
  return carsByYear;
}, new Map());

const output = [...carsByYearAndMake.entries()].map(
  ([year, fleetCountByMake]) => ({ year: year, ...fleetCountByMake })
);
console.log(JSON.stringify(output, null, 4));
/* StackOverflow snippet: console should overlap rendered HTML area */
.as-console-wrapper { max-height: 100% !important; top: 0; }

almost 4 years ago · Santiago Trujillo 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!