Group an array of objects

0

I'm trying to group an array of objects coming from the database in the following format:

[
       {
          '12312312': { 'first_name': 'David', 'id_question': 0, 'acertou': 1 },
          '23423423': { 'first_name': 'Luis', 'id_question': 0, 'acertou': 1 },
          '45645645': { 'first_name': 'José', 'id_question': 0, 'acertou': 1 }
       },
       {
          '12312312': { 'first_name': 'David', 'id_question': 1, 'acertou': 1 },
          '23423423': { 'first_name': 'Luis', 'id_question': 1, 'acertou': 0 },
          '45645645': { 'first_name': 'José', 'id_question': 1, 'acertou': 1 }
       },
       {
          '12312312': { 'first_name': 'David', 'id_question': 2, 'acertou': 1 },
          '23423423': { 'first_name': 'Luis', 'id_question': 2, 'acertou': 1 },
          '45645645': { 'first_name': 'José', 'id_question': 2, 'acertou': 0 }
       }
]

I need to transform it to the following format:

[
    {
        first_name: "David",
        resultados: [
            { "id_question": 0, "acertou": 1 },
            { "id_question": 1, "acertou": 1 },
            { "id_question": 2, "acertou": 1 }
        ]
     },
     {
         first_name: "Luis",
         resultados: [
            { "id_question": 0, "acertou": 1 },
            { "id_question": 1, "acertou": 0 },
            { "id_question": 2, "acertou": 1 }
         ]
     },
     {
          first_name: "José",
          resultados: [
            { "id_question": 0, "acertou": 1 },
            { "id_question": 1, "acertou": 1 },
            { "id_question": 2, "acertou": 0 }
          ]
     }
]

Could someone please help me?

Thank you.

    
asked by anonymous 27.12.2017 / 23:24

2 answers

0

Dude, I do not even need to create another array, use .forEach or .indexOf and good. For example:

var arrTemp = JSON.parse(resposta_do_banco);
arrTemp.forEach(fillResumeArr);

function fillResumeArr(item, index)
{
    //como seus subitens são arrays, verifico se o item é um array, e boa
    if(item.isarray()){
        //faz o que quiser com o item.
    }
}
    
28.12.2017 / 02:35
0

I solved as follows using lodash's groupBy:

        let itt = []; // Recebe os valores do banco de dados             
        let arr1 = [];

         for (let i = 0; i < itt.length; i++) {
            arr1.push(Object.values(itt[i]))
         }

         let arr2 = [];

         for (let j = 0; j < arr1.length; j++) {
            for (let z = 0; z < Object.values(arr1[j]).length; z++) {
               arr2.push(arr1[j][z])
            }
         }

         let arr3 = _.groupBy(arr2, 'first_name')

         let arr = []

         Object.keys(arr3).map(function (objectKey, index) {
            let value = arr3[objectKey];
            let val1 = {
               first_name: objectKey,
               respostas: value
            }
            arr.push(val1)
         });

         this.itemsFiltered = arr; // Valores no formato que desejo enviar para o front

It worked for me.

    
28.12.2017 / 13:38