Home > Blockchain >  Group items in an array by a specific key value in JavaScriot
Group items in an array by a specific key value in JavaScriot

Time:11-09

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']
        ]}]

CodePudding user response:

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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]]
    }
}
  • Related