In my code, I have a model that is being represented by a class.
For example, let's consider the model of a car:
export class Car
{
public name : string;
public color: string;
public power : number;
public service : boolean;
}
All cars are stored in a list.
cars : Array <Car>;
Now, I am looking to create a function that can modify one attribute of a car dynamically.
For instance, changing the color of the first car or the name of the second car.
What would be the most efficient way to pass a reference to a specific car and its attribute as parameters for this function?
From what I understand, using something like
cars[0].name
would only pass the value, not a reference within the function.
I can target a car through the index of the array, but for the attribute, using a string for interpretation doesn't seem very optimal. Do you have any better suggestions for how to approach this problem?