There are a couple of problems there, but the fundamental thing is that while you're creating a new array, you're not creating new objects. So both arrays contain the same objects, so when you change the state of one of those objects, you see that change regardless of which array you look in.
To make a shallow copy of an object, you can use spread notation:
var temp = cars.map(obj =>({...obj}));
That creates a new array and new objects in the array.
Separately: Don't use map
if you're not going to use the array it returns. Someone, somewhere, is incorrectly teaching people to use map
just to loop through things. It's not the right tool for that (use any of these options instead).
In your case, though, since you did want a new array, map
is a good choice, just use it as above to directly create your array, rather than using just for the loop.