So i have an json array named users. Im trying to update an individual field if id comes from request equals to users id. When i use for loop it works but it doesn't when i try to use forEach (there are no errors). I don't understand the difference between these two.
Asked
Active
Viewed 48 times
1

hakanAkdogan
- 223
- 2
- 3
- 10
-
1[This](https://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value) may be helpful – Rafs Oct 14 '20 at 16:00
1 Answers
1
In the forEach
, user
is just a variable that happens to start with the value passed in as a parameter to your lambda function. Setting user = ...
doesn't actually change anything in the original array. Nor does it change the properties on the object that is currently in that array.
Consider using Object.assign()
instead:
Object.assign(user, req.body);

StriplingWarrior
- 151,543
- 27
- 246
- 315