0

I'm trying to add dynamically created objects inside an object, however it overrides the properties in each iteration in the forEach ... I would like to create an object equal to the image with the GREEN color, the RED color is what it is returning to me now..

NOTE: I do not want to create an array, but instead create keys and values ​​dynamically and add in the given object to insert into a SET afterwards.

example

   static organizeData(){ 
        let organizedData = {};

        let oi = {
            "NAME1": {},
            "NAME2": {}
        }

        console.log(oi)

        API.data.records.forEach(record => {
            record.INDICADORES.forEach(indicadores => {
                if(indicadores.tipo === API.queryIndicatorType){
                    indicadores[indicadores.tipo].KPIS.forEach(kpi => {
                        organizedData = { [kpi.tipo]: kpi[kpi.tipo] }
                    });
                }
            });
        });

        API.organizedData = organizedData;
        console.log(organizedData)
    }

1 Answers1

0

You shouldn't assign to organizedData, that replaces the entire object. You should just assign to the specific property you want to create. So change:

organizedData = { [kpi.tipo]: kpi[kpi.tipo] }

to:

organizedData[kpi.tipo] = kpi[kpi.tipo];
Barmar
  • 741,623
  • 53
  • 500
  • 612