0

i have a function called in ngInit(), that fills an array with objects of a class with values generated in a for loop, using array.push().

However, once the for loop completes, all the array items are the last-added instance of the class.

Class def

class ABC{
time: number;
id: number;
 }

Function def

addToArray(){
 let arrayTemp : ABC[]= [];
 let tempClass ={} as ABC;

 for (let i = 0; i < 4; i++) {
 
   tempClass.timei+1;
   tempClass.id=i;

    let val=i;
    arrayTemp.push(tempClass);
    console.log(arrayTemp[val]);   // here class objects displayed correctly
}

   console.log(arrayTemp);  // here all elements are just the last added class object
  
  }
 }

Output of console.log(arrayTemp[val])

enter image description here

Output of console.log(arrayTemp) after the for loop

enter image description here

I'm not sure why this happens, any help would be appreciated.

5
  • 4
    each element in the array contains the same instance of the ABC class. So each time you update the tempClass values, each element will reflect the change. Commented Jun 17, 2021 at 13:58
  • 1
    put let tempClass ={} as ABC; inside the for loop if you want a new instance of the class with each iteration of the for loop. Commented Jun 17, 2021 at 14:00
  • @cWerning Why have you answered it in a comment? Just post the answer. Commented Jun 17, 2021 at 14:01
  • 1
    @AustinTFrench cuz I don't follow rules from the man Commented Jun 17, 2021 at 14:03
  • 1
    Javascript is not C++, read the docs on what a function does, does it make a copy of the argument or not Commented Jun 17, 2021 at 14:33

1 Answer 1

5

When you modify Class directly, like here

   tempClass.timei+1;
   tempClass.id=i;

You are modifying the existing class instance.
Because you add this already existing instance in array with
arrayTemp.push(tempClass);
It means you edit the class you created in the beginning with
let tempClass ={} as ABC;

What you actually want to do, is create a new class instance for each iteration, like so

addToArray(){
 let arrayTemp : ABC[]= [];

 for (let i = 0; i < 4; i++) {
   // create new instance of class for each iteration
   let tempClass = new ABC();
   tempClass.timei+1;
   tempClass.id=i;

    let val=i;
    arrayTemp.push(tempClass);
    console.log(arrayTemp[val]);   // here class objects displayed correctly
}

   console.log(arrayTemp);  // here all elements are just the last added class object
  
  }
 }

Edit:
Don't forget to create your class with new keyword! If you do intialization directly as let tempClass ={} as ABC; you wont get actual representation of a class, you only get empty object that only looks like a ABC class to typescript.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, fairly new to typescript, so this explanation helped tons!

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.