I am trying to replace an element in a list by a new one (data). The list is in my scope as $scope.list.
So I do something like that :
angular.foreach($scope.list, function(element) {
if(element.id === data.id) {
element = data;
console.log(element);
return false;
});
console.log($scope.list);
In the console element is updated but $scope.list remains unchanged.
First I tried to use Array.find() instead of looping the list like this :
$scope.list.filter(x => x.id === data.id)[0] = data;
But I got an error in the console : Invalid left-hand side in assignment.
Do you know why it doesn't change the $scope.list ? I am quite new in AngularJS and JavaScript.
Thanking you in advance,
element=datais only changing a variable in memory. You need to change the value of the array element at the index where the id's match