-2

I have this array, that I want to transform into the array below so we can have a shorted and clearer object

const data = [
        {
            "horarioHeader": {
                "hr_id": 105,
                "hr_nombre": "Prueba 2",
                "hr_id_empresa": 14,
            },
            "horarioDetalles": [
                {
                    "hrd_id": 4,
                    "hrd_hr_id": 105,
                    "hrd_nombre": "Prueba detalle 2",
                    "hrd_dia_semana_ini": null,
                    "hrd_dia_mes_ini": null,
                    "hrd_mes_ini": null,
                    "hrd_anio_ini": null
                }
            ]
        },
        {
            "horarioHeader": {
                "hr_id": 105,
                "hr_nombre": "Prueba 2",
                "hr_id_empresa": 14,
            },
            "horarioDetalles": [
                {
                    "hrd_id": 5,
                    "hrd_hr_id": 105,
                    "hrd_nombre": "Prueba detalle 3",
                    "hrd_dia_semana_ini": null,
                    "hrd_dia_mes_ini": null,
                    "hrd_mes_ini": null,
                    "hrd_anio_ini": null
                }
            ]
        },
        {
            "horarioHeader": {
                "hr_id": 105,
                "hr_nombre": "Prueba 2",
                "hr_id_empresa": 14
            },
            "horarioDetalles": [
                {
                    "hrd_id": 8,
                    "hrd_hr_id": 105,
                    "hrd_nombre": "Prueba prueba",
                    "hrd_dia_semana_ini": null,
                    "hrd_dia_mes_ini": null,
                    "hrd_mes_ini": null,
                    "hrd_anio_ini": null,
                }
            ]
        },
        
    ]
}

if you notice horarioHeader information is repeated I would like to map this array and get something like this

const dataFormated = [{
                "horarioHeader": {
                    "hr_id": 105,
                    "hr_nombre": "Prueba 2",
                    "hr_id_empresa": 14,
                },
                "horarioDetalles": [
                    {
                        "hrd_id": 4,
                        "hrd_hr_id": 105,
                        "hrd_nombre": "Prueba detalle 2",
                        "hrd_dia_semana_ini": null,
                        "hrd_dia_mes_ini": null,
                        "hrd_mes_ini": null,
                        "hrd_anio_ini": null
                    },
                    {
                        "hrd_id": 5,
                        "hrd_hr_id": 105,
                        "hrd_nombre": "Prueba detalle 3",
                        "hrd_dia_semana_ini": null,
                        "hrd_dia_mes_ini": null,
                        "hrd_mes_ini": null,
                        "hrd_anio_ini": null
                    },
                    {
                        "hrd_id": 8,
                        "hrd_hr_id": 105,
                        "hrd_nombre": "Prueba prueba",
                        "hrd_dia_semana_ini": null,
                        "hrd_dia_mes_ini": null,
                        "hrd_mes_ini": null,
                        "hrd_anio_ini": null
                    }]
}];

I have been trying but I still haven't been able to get the correct algorithm, if anyone can help me here.

Here it's my function

const findDuplicates = (arr) => {
            
            let results = [];
            for (let i = 0; i < arr.length - 1; i++) {
              if (arr[i + 1].horarioHeader.hr_id === arr[i].horarioHeader[0].hr_id ) {
                results.push({ horarioHeader:[{...arr[i].horarioHeader}], horarioDetalles:[...arr[i].horarioDetalles, ...arr[i + 1].horarioDetalles] });
              }
              else{
                results.push({ horarioHeader:[{...arr[i].horarioHeader}], horarioDetalles:[...arr[i].horarioDetalles]});
              }
            }
        return results;
      }
  • Could you share the code you have been trying as a [mre]? – Rojo Oct 13 '21 at 18:32
  • Does this answer your question? [Remove duplicate values from JS array](https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array) – jsonderulo Oct 13 '21 at 18:33

1 Answers1

0

I came out with this solution

let output = data.reduce((accumalator, currentValue) => {  
            let entry = accumalator.find(item => item.horarioHeader.hr_id === currentValue.horarioDetalles[0].hrd_hr_id);
            if (entry) { 
                    entry.horarioDetalles = [...entry.horarioDetalles, ...currentValue.horarioDetalles];
                }
            else  {
              accumalator.push({horarioHeader: currentValue.horarioHeader, horarioDetalles: [currentValue.horarioDetalles]}); //For the 1st time, add it to the array
            }
            return accumalator;
        }, []);