Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.push not working within for loop within forEach loop.Javascript

I have been trying to push an array of 3 values to an array to create an AOA. However my expected output is not coming out correct.

Input data looks something like the following

[
{id:xxxxx,
date:VALUE,
date:VALUE,
..months worth
},
{id:xxxx2,
date:VALUE,
date:VALUE,
..months worth
},
..loads of entries
]

please see the snip of code below.

arrayOfObjects.forEach(e =>{            
            let entry = [];
            entry[0] = e.id;            
            let PC = Object.values(e)        
            let Dates = Object.keys(e)
            PC.shift();
            Dates.shift();
            for(let i = 0; i <PC.length; i++){
                if(PC[i] === 'No Data'){
                    entry[2] = 0;
                    entry[1] = Dates[i];                   
                } else {
                    entry[2] = PC[i];
                    entry[1] = Dates[i];
                }
                finalArray.push(entry);
            }

        })

I am getting an array of outputs that contain the a static date

[
  ...
  [ 'NAAAAD', '10/31/19', 0 ],
  [ 'NAAAAD', '10/31/19', 0 ],
  [ 'NAAAAD', '10/31/19', 0 ],
  [ 'NAAAAD', '10/31/19', 0 ],
  [ 'NAAAAD', '10/31/19', 0 ],
  [ 'NAAAAD', '10/31/19', 0 ],
  [ 'NAAAAD', '10/31/19', 0 ],
  ...
]

When i want to see the date increment.

[
  ...
  [ 'NAAAAD', '10/25/19', 0 ],
  [ 'NAAAAD', '10/26/19', 0 ],
  [ 'NAAAAD', '10/27/19', 0 ],
  [ 'NAAAAD', '10/28/19', 0 ],
  [ 'NAAAAD', '10/29/19', 0 ],
  [ 'NAAAAD', '10/30/19', 0 ],
  [ 'NAAAAD', '10/31/19', 0 ],
  ...
]

I have console.log(entry) just before it gets pushed into the array and i contains the correct information at that point? I have moved the finalArray.push(entry) to be within the if statements directly under the line where entry is correct.

What is going on here? I am unable to find anything like this, I've considered the scope of variables, but they all seems correct.

Thanks

Ryan

like image 939
Peachman1997 Avatar asked Jan 17 '20 12:01

Peachman1997


2 Answers

I think you might need to clone the object every time before pushing it:

arrayOfObjects.forEach(e =>{            
        let entry = [];
        entry[0] = e.id;            
        let PC = Object.values(e)        
        let Dates = Object.keys(e)
        PC.shift();
        Dates.shift();
        for(let i = 0; i <PC.length; i++){
            let newEntry = Object.assign([], entry);
            if(PC[i] === 'No Data'){
                newEntry[2] = 0;
                newEntry[1] = Dates[i];                   
            } else {
                newEntry[2] = PC[i];
                newEntry[1] = Dates[i];
            }
            finalArray.push(newEntry);
        }

    })
like image 181
TKoL Avatar answered Sep 28 '22 09:09

TKoL


instead of this complicated approach why don't you just create a new, distinct entry for every new distinct row you want to add to the list.

arrayOfObjects.forEach(e => {
  for (let [key, value] of Object.entries(e)) {
    if (value === 'No Data') {
      value = 0; // change the variable `value`
    }
    // push a new row
    finalArray.push([ e.id, key, value ]);
  }
})

or

arrayOfObjects.forEach(e => {
  for (let [key, value] of Object.entries(e)) {
    finalArray.push([ e.id, key, value === 'No Data' ? 0 : value ]);
  }
})
like image 39
Thomas Avatar answered Sep 28 '22 07:09

Thomas