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

108
Visualizações
Nodejs: Object aggregation

I am using Nodejs & would like to group my object array based on few attributes. example attr_id & type will be unique

const normalizedList =[ {
        "attr_id": 1,
        "type": "color",
        "value_index": 10,
        "value_label": "Blue"
    },
    {
        "attr_id": 1,
        "type": "color",
        "value_index": 15,
        "value_label": "Red"
    },
    {
        "attr_id": 2,
        "type": "size",
        "value_index": 10,
        "value_label": "Small"
    },
    {
        "attr_id": 2,
        "type": "size",
        "value_index": 14,
        "value_label": "Big"
    }
];

Needs to be converted to

[{
        "attr_id": 1,
        "type": "color",
        "values": [{
                "index": 10,
                "label": "Blue"
            }, {
                "index": 15,
                "label": "Red"
            }
        ]
    }, {
        "attr_id": 2,
        "type": "size",
        "values": [{
                "index": 10,
                "label": "Small"
            }, {
                "index": 14,
                "label": "Big"
            }
        ]
    }
]

I was trying to do this without any packages like "underscore" or "json-aggregate". as not many places in the code we do any grouping/aggregation

Below is how I was able to solve

const groupJson = (arr,g_label) =>{
// group the list by 'attr_id'
    const groupedAttrList = normalizedList.reduce((acc, obj)=>{
      let key = obj[g_label]
      if (!acc[key]) {
        acc[key] = []
      }
      acc[key].push(obj)
      return acc
    },{});
    const finalArray = [];

  // Now iterate over the groupedAttrList 
    for (let typeRow in groupedAttrList){
      let resultObj = {};
      let tempRow = groupedAttrList[typeRow];
      // as attr_id,type are unique for the set; picking it from 1st
      const {attr_id,type} = tempRow [0];
      resultObj .attr_id= attr_id;
      resultObj .type= type;
      // get all the values
      resultObj .values = tempRow .map(v=>{
        return {index:v.value_index,label:v.value_label};
      });
      finalArray.push(resultObj );
    }
    console.log(finalArray);
};

Test

let tempResult = groupJson(normalizedList,'attr_id');

wanted to learn if there are better ways to do it

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Well I guess you can make it more dynamic(something to group by an unknown amount of attributes)

First, I give you the answer in your output format in structure
[{values:[...objectsInGroup],...groupingInfo}, ...otherGroupObjects]

const myList=[{"attr_id":1,"type":"color","value_index":10,"value_label":"Blue"},{"attr_id":1,"type":"color","value_index":15,"value_label":"Red"},{"attr_id":2,"type":"size","value_index":10,"value_label":"Small"},{"attr_id":2,"type":"size","value_index":14,"value_label":"Big"}]
function groupList(list,attributes){
  var cache={} //for finding elements that have the same attributes
  for(let item of list){
    let ID=attributes.map(attr=>item[attr]).join('-')
    //items with same ID would be grouped together
    cache[ID]? cache[ID].values.push(item): cache[ID]={values:[item]}
    //if ID exists.. add to the group, else make the group
    attributes.forEach(key=>{
      if(!cache[ID][key]){ cache[ID][key]=item[key] }
    })
  }
  return Object.values(cache)
}

const newList=groupList(myList,['attr_id','type'])
console.log(newList)

But what if there was a values attribute that is in the original items in the list that you wanted it sorted by? only one value can exist per key.. in that case you can change the structure to
[{values:[...objectsInGroup],info:{...groupingInfo}} ...otherGroupObjects]

const myList=[{"attr_id":1,"type":"color","value_index":10,"value_label":"Blue"},{"attr_id":1,"type":"color","value_index":15,"value_label":"Red"},{"attr_id":2,"type":"size","value_index":10,"value_label":"Small"},{"attr_id":2,"type":"size","value_index":14,"value_label":"Big"}]
function groupList(list,attributes){
  var cache={} //for finding elements that have the same attributes
  for(let item of list){
    let ID=attributes.map(attr=>item[attr]).join('-')
    //items with same ID would be grouped together
    cache[ID]? cache[ID].values.push(item): cache[ID]={values:[item]}
    //if ID exists.. add to the group, else make the group
    if(!cache[ID].info){
      cache[ID].info={} //info about the grouping of this array
      attributes.forEach(key=>cache[ID].info[key]=item[key])
    }
  }
  return Object.values(cache)
}

const newList=groupList(myList,['attr_id','type'])
console.log(newList)

about 4 years ago · Juan Pablo Isaza Relatório

0

Im not necessarily sure if this is any better, but this is how I would maybe go about this. You could easily wrap this into a function

const finalObjects = [];
const checkIdInArray = (id) => finalObjects.find((obj) => obj.attr_id === id);

normalizedList.forEach((element) => {
  if (!checkIdInArray(element.attr_id)) {
    // Then add to array as its not been added yet
    finalObjects.push({
      attr_id: element.attr_id,
      type: element.type,
      values: [
        {
          index: element.value_index,
          label: element.value_label,
        },
      ],
    });
    return;
  }
  // Get the index of the existing id
  const arrIndex = finalObjects.findIndex((e) => e.attr_id === element.attr_id);
  finalObjects[arrIndex].values.push({
    index: element.value_index,
    label: element.value_label,
  });
});
console.log(JSON.stringify(finalObjects));
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