+ 1
What are updated array values not saved to array in for each loop
2 odpowiedzi
+ 2
because the 'element' variable only hold the value of each items, not the array reference ^^
to update the array using forEach method, use other callback arguments:
arr.forEach((value,index,array) => {
if (value>15) array[index] += 2;
});
+ 1
however, you could assign to arr a mapping of arr:
arr = arr.map(
val => val>15 ? val + 2 : val
);