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.

6
  • Well, you are putting the reference to the same object (this.data) multiple times on the array. Maybe, you can create a new object inside the loop (not using this.data for store the values) and then push this new object inside this.dataSource Commented May 29, 2019 at 4:03
  • You have to clone the object. Commented May 29, 2019 at 4:03
  • How do you clone an object? Commented May 29, 2019 at 4:03
  • stackoverflow.com/questions/122102/… Commented May 29, 2019 at 4:04
  • So you would close the Object after the push or before the push? Commented May 29, 2019 at 4:06

3 Answers 3

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);
    }
Sign up to request clarification or add additional context in comments.

Comments

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);
    };

1 Comment

That code only works if I move the const temp inside the for loop.
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));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.