sortDates(){
let index = 0;
for(let year of this.yearList){
let yearInfo = this.yearInfo;
this.year.push(yearInfo)
index++;
}
this.year[2].year = '123'
Versus
sortDates(){
let index = 0;
for(let year of this.yearList){
let yearInfo {
year: '',
info: [
{
date:'Dec',
total: 0
},
{
date:'Nov',
total: 0
},
{
date:'Oct',
total: 0
}
]
};
this.year.push(yearInfo)
index++;
}
The second one allows me to change the values of 'year' without changing it for all elements. The first changes every element to the same value.
Example: if 2016 was the last value in the for loop, all the values in the array will be 2016 for 'year'
0:{year: "123", info: Array(12)}
1:{year: "123", info: Array(12)}
2:{year: "123", info: Array(12)}
Versus
0:{year: "", info: Array(12)}
1:{year: "", info: Array(12)}
2:{year: "123", info: Array(12)}
Is there a better way to obtain the 2nd result? and why does the first result happen? thanks!
EDIT : Using Object.assign helped