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
How to group certain objects in array of objects?
var list = [{
        'orderSeries': 26342,
        'make': 'audi',
        'model': 'r8',
        'year': '2012'
    }, {
        'orderSeries': 26342,
        'make': 'audi',
        'model': 'rs5',
        'year': '2013'
    }, {
        'orderSeries': null,
        'make': 'ford',
        'model': 'mustang',
        'year': '2012'
    }, {
        'orderSeries': null,
        'make': 'ford',
        'model': 'fusion',
        'year': '2015'
    }, {
        'orderSeries': null,
        'make': 'kia',
        'model': 'optima',
        'year': '2012'
    }];

Is it possible to group the first 2 objects in this array because the orderSeries value is the same, and the rest of the objects to not be grouped by orderSeries property because the value for those is null?

and to get a format of the array like this when its grouped:

var list = [{
        'orderSeries': 26342,
        'make': 'audi',
        cars: [
            { 'model': 'r8', 'year': '2012'},
            {'model': 'rs5', 'year': '2013'}
        ]
    }, 
    {
        'orderSeries': null,
        'make': 'ford',
        'model': 'mustang',
        'year': '2012'
    }, {
        'orderSeries': null,
        'make': 'ford',
        'model': 'fusion',
        'year': '2015'
    }, {
        'orderSeries': null,
        'make': 'kia',
        'model': 'optima',
        'year': '2012'
    }]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I would use just forEach and fill new list with some modifications

Edit elegant-mcclintock-c9h77

const list = [{
  'orderSeries': 26342,
  'make': 'audi',
  'model': 'r8',
  'year': '2012'
}, {
  'orderSeries': 26342,
  'make': 'audi',
  'model': 'rs5',
  'year': '2013'
}, {
  'orderSeries': null,
  'make': 'ford',
  'model': 'mustang',
  'year': '2012'
}, {
  'orderSeries': null,
  'make': 'ford',
  'model': 'fusion',
  'year': '2015'
}, {
  'orderSeries': null,
  'make': 'kia',
  'model': 'optima',
  'year': '2012'
}];

const groupedList = [];

list.forEach(item => {
  if (!item.orderSeries) {
    groupedList.push(item);
  } else {
    const existingGroup = groupedList.find(i => i.orderSeries === item.orderSeries);

    if (existingGroup) {
      existingGroup.cars.push({ model: item.model, year: item.year });
    } else {
      groupedList.push({
        orderSeries: item.orderSeries,
        make: item.make,
        cars: [
          { model: item.model, year: item.year }
        ]
      });
    }
  }
});

console.log(groupedList);
about 4 years ago · Juan Pablo Isaza Report

0

  • Using Array#reduce, iterate over list while updating two arrays, one for orders with orderSeries and another without it
  • Using Array#reduce again, iterate over the first list with orderSeries while updating a Map to group the items by the orderSeries
  • The resulting list will include the grouped list and the ones without orderSeries

const list = [ { 'orderSeries': 26342, 'make': 'audi', 'model': 'r8', 'year': '2012' }, { 'orderSeries': 26342, 'make': 'audi', 'model': 'rs5', 'year': '2013' }, { 'orderSeries': null, 'make': 'ford', 'model': 'mustang', 'year': '2012' }, { 'orderSeries': null, 'make': 'ford', 'model': 'fusion', 'year': '2015' }, { 'orderSeries': null, 'make': 'kia', 'model': 'optima', 'year': '2012'  }];

const [ordersWithSeries, ordersWithoutSeries] = list.reduce(([a, b], order) => 
  order.orderSeries ? [[...a, order], b] : [a, [...b, order]]
, [[], []]);

const groupedOrdersBySeries = [...
  ordersWithSeries.reduce((orderSeriesMap, { orderSeries, make, model, year }) => {
    const order = orderSeriesMap.get(orderSeries);
    if(!order) {
      orderSeriesMap.set(orderSeries, { orderSeries, make, cars: [{ model, year }] });
    } else {
      order.cars.push({ model, year });
    }
    return orderSeriesMap;
  }, new Map)
  .values()
];

const res = [...groupedOrdersBySeries, ...ordersWithoutSeries ];

console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

I would simply manipulate original data one by one using for loop.

const list = [{
    orderSeries: 26342,
    make: "audi",
    model: "r8",
    year: "2012"
  },
  {
    orderSeries: 26342,
    make: "audi",
    model: "rs5",
    year: "2013"
  },
  {
    orderSeries: null,
    make: "ford",
    model: "mustang",
    year: "2012"
  },
  {
    orderSeries: null,
    make: "ford",
    model: "fusion",
    year: "2015"
  },
  {
    orderSeries: null,
    make: "kia",
    model: "optima",
    year: "2012"
  }
];

let mergeIndexes = [];
let newList = [];

const placeholder = {
  orderSeries: null,
  make: "",
  cars: []
};

for (let i = 0; i <= list.length - 1; i++) {

  // continue if current index is already merge with the previous
  if (mergeIndexes.includes(i.toString())) continue;
  const item = list[i];
  const filter = list.filter((i) => i.orderSeries === item.orderSeries);
  if (filter.length && Boolean(item.orderSeries)) {
    placeholder.orderSeries = filter[0].orderSeries;
    placeholder.make = filter[0].make;
    placeholder.cars = filter.reduce(
      (prev, curr) => [...prev, {
        model: curr.model,
        year: curr.year
      }], []
    );
    newList = [...newList, placeholder];
    // to keep track of the indexes that are already merge
    mergeIndexes = [...mergeIndexes, ...Object.keys(filter)];
  } else {
    newList = [...newList, item];
  }
}
console.log(newList);

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!