Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

230
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda