0

I have this for loop that I want to increment the year on every cycle, but I'm only getting the last year all repeating multiple times.

for (let i = 0; i < 2; i++) {
  this.data.year = new Date().getFullYear() + i;
  this.data.noSolar = averageBill * increaseRate;
  this.data.withSolar = (contractAmount * .004) + customerCharge;
  this.data.saving = (contractAmount * .004 + customerCharge) * 12 - (averageBill * 12);
  this.data.check = SREC;
  this.data.total = (contractAmount * .004 + customerCharge) * 12 - (averageBill * 12) + SREC;

  this.dataSource.push(this.data);
}

The Year in this case 2020 is showing twice. I want something like 2019 and 2020. Its like the variable is being reference multiple times.

Millenial2020
  • 2,465
  • 9
  • 38
  • 83

3 Answers3

7

A new object should be created on each iteration. You are referring to the same object every time.

You can do like this,

for (let i = 0; i < 2; i++) {
  this.dataSource.push({
     year : new Date().getFullYear() + i,
     noSolar : averageBill * increaseRate,
     withSolar : (contractAmount * .004) + customerCharge,
     saving : (contractAmount * .004 + customerCharge) * 12 - (averageBill * 12),
     check : SREC,
     total : (contractAmount * .004 + customerCharge) * 12 - (averageBill * 12) + SREC,
  });
}

or do like,

for (let i = 0; i < 2; i++) {
      this.data=new DataSourceObject();
      this.data.year = new Date().getFullYear() + i;
      this.data.noSolar = averageBill * increaseRate;
      this.data.withSolar = (contractAmount * .004) + customerCharge;
      this.data.saving = (contractAmount * .004 + customerCharge) * 12 - (averageBill * 12);
      this.data.check = SREC;
      this.data.total = (contractAmount * .004 + customerCharge) * 12 - (averageBill * 12) + SREC;

      this.dataSource.push(this.data);
    }
Hardik Chaudhary
  • 1,200
  • 1
  • 8
  • 24
1

Reference of object is pushed in array. Instead clone or create copy and then push

    const temp = {};
    for (let i = 0; i < 2; i++) {
      this.data.year = new Date().getFullYear() + i;
      this.data.noSolar = averageBill * increaseRate;
      this.data.withSolar = (contractAmount * .004) + customerCharge;
      this.data.saving = (contractAmount * .004 + customerCharge) * 12 - (averageBill * 12);
      this.data.check = SREC;
      this.data.total = (contractAmount * .004 + customerCharge) * 12 - (averageBill * 12) + SREC;
      // this.dataSource.push(...this.data)
      Object.assign(temp, this.data);
      this.dataSource.push(temp);
    };
random
  • 7,756
  • 3
  • 19
  • 25
0

You may try this:-

for (let i = 0; i < 2; i++) {
  this.data.year = new Date().getFullYear() + i;
  this.data.noSolar = averageBill * increaseRate;
  this.data.withSolar = (contractAmount * .004) + customerCharge;
  this.data.saving = (contractAmount * .004 + customerCharge) * 12 - (averageBill * 12);
  this.data.check = SREC;
  this.data.total = (contractAmount * .004 + customerCharge) * 12 - (averageBill * 12) + SREC;

  this.dataSource.push(Object.assign({}, this.data));
}
Manish Balodia
  • 1,863
  • 2
  • 23
  • 37