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

227
Views
Group items in an array by a specific key value in JavaScriot

Below is my Json Object.

[{section: 'group_date', category: 'Support', category_value: '2021-10-11', datavalue: '2'}
{section: 'group_date', category: 'Support', category_value: '2021-10-20', datavalue: '3'}
{section: 'group_date', category: 'Support', category_value: '2021-11-02', datavalue: '1'}
{section: 'group_date', category: 'Dev', category_value: '2021-11-02', datavalue: '1'}
{section: 'group_date', category: 'Dev', category_value: '2021-11-03', datavalue: '1'}]

I am trying to create a new array, like the one listed below.

 [
{category:'Support', value:[
        ['2021-10-11','2'],
        ['2021-10-20','3'],
        ['2021-11-02','1'] 
        ]},
    {category:'Dev', value[
        ['2021-11-02','1'],
        ['2021-11-03','1']
        ]}]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could do it like this using some destructuring and Array#reduce:

let data = [ { section: 'group_date', category: 'Support', category_value: '2021-10-11', datavalue: '2', }, { section: 'group_date', category: 'Support', category_value: '2021-10-20', datavalue: '3', }, { section: 'group_date', category: 'Support', category_value: '2021-11-02', datavalue: '1', }, { section: 'group_date', category: 'Dev', category_value: '2021-11-02', datavalue: '1', }, { section: 'group_date', category: 'Dev', category_value: '2021-11-03', datavalue: '1', }, ];

let seen = [];
let res = data.reduce((acc, { category, category_value, datavalue }) => {
  let index = seen.indexOf(category);
  if (index === -1) {
    seen.push(category);
    acc.push({ category, value: [[category_value, datavalue]] });
  } else {
    acc[index].value.push([category_value, datavalue]);
  }
  return acc;
}, []);

console.log(res);

Or like this using a for...of loop instead:

let data = [ { section: 'group_date', category: 'Support', category_value: '2021-10-11', datavalue: '2', }, { section: 'group_date', category: 'Support', category_value: '2021-10-20', datavalue: '3', }, { section: 'group_date', category: 'Support', category_value: '2021-11-02', datavalue: '1', }, { section: 'group_date', category: 'Dev', category_value: '2021-11-02', datavalue: '1', }, { section: 'group_date', category: 'Dev', category_value: '2021-11-03', datavalue: '1', }, ];

let res = [];
let seen = [];

for ({ category, category_value, datavalue } of data) {
  let index = seen.indexOf(category);
  if (index === -1) {
    seen.push(category);
    res.push({ category, value: [[category_value, datavalue]] });
  } else {
    res[index].value.push([category_value, datavalue]);
  }
}

console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

I think using a for loop here should be fine. Something like:

let newObject = {};

let originalList = [{section: 'group_date', category: 'Support', category_value: '2021-10-11', datavalue: '2'}
{section: 'group_date', category: 'Support', category_value: '2021-10-20', datavalue: '3'}
{section: 'group_date', category: 'Support', category_value: '2021-11-02', datavalue: '1'}
{section: 'group_date', category: 'Dev', category_value: '2021-11-02', datavalue: '1'}
{section: 'group_date', category: 'Dev', category_value: '2021-11-03', datavalue: '1'}]

originalList.forEach(item => {
    if (item.category in newObject) {
        newObject[item.category].push([item.category_value, item.datavalue])
    }
    else {
        newObject[item.category] = [[item.category_value, item.datavalue]]
    }
}
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!