I have a javascript class like this:
class transportDetails{
constructor(){
this.car_data = [[3,0]];
this.bike_data = [[7,2]];
this.van_data = [[1,0]];
this.train_data = [[9,3]];
}
}
transport = new transportDetails();
This is fine and works ok. But how can I perform a loop through the property names and obtain their individual values?
for(n=0; n<=3; n++){
a2 = transport.transportDetails(Object.getOwnPropertyNames(race));
}
But instead of just getting the property names, i.e, car_data, bike_data etc. I would also get the value of the two dimensional array?
Thanks in advance.
Do this way :
class transportDetails{
constructor(){
this.car_data = [[3,0]];
this.bike_data = [[7,2]];
this.van_data = [[1,0]];
this.train_data = [[9,3]];
}
}
transport = new transportDetails();
Object.keys(transport).forEach(key=>{
console.log("key is. : ",key);
console.log("value is. : ",transport[key]);
})