I have a JS Array as follows:
const cars = [
{
make : "Toyota",
model : "Corolla",
year : 2022,
active : "false"
},
{
make : "Tesla",
model : "Model 3",
year : 2021,
active : "true"
}
];
if I use:
let filteredCars = cars.filter((cars) => {
return cars.active == "true";
});
i then update filteredCars.active = "false"
does it update the cars array? or just filteredCars ?
The reason i ask is if i use index as the key to update the array won't it be the wrong index? it will be 1 in cars but 0 in filteredCars....
so, how do I ensure that cars is updated correctly?
carsthe array andcarsthe filter predicate parameter), making an assignment (active="true") where you probably meant a comparison using==, referring to the undefinedactivevariable (you probably meantcars.active), and assigning a random property to an array (filteredCars.active="false"). Please edit your question for clarityArray.prototype.filter()runs once at call time. How you would implement such a thing is a pretty broad topicfilteredCars.active="false"" 👈 this doesn't make sense. Did you meanfilteredCars[0].active = "false"or similar? If so, then that will update the same object reference in each array. But that doesn't really align with what you're asking after that.filteredCars[0].active="false". willcars[0]be updated orcars[1]?