I am working on a TypeScript class that includes 2 private properties and an array of objects.
class AssigningProperties {
private animalType1: string;
private animalType2: string;
animals = [
{
name: "Bob",
type: "cat"
},
{
name: "Max",
type: "dog"
}
];
constructor() {
this.animals.forEach(v => console.log(v.name));
}
}
new AssigningProperties ();
I am trying to assign the values 'cat' and 'dog' to the class properties animalType1
and animalType2
, respectively, using the Array.foreach
method.
Currently, I can achieve this by accessing the objects through their index like so:
this.animalType1 = this.animals[0].type;
but I believe there might be a more efficient approach. Any suggestions?