When using the .map()
method to iterate over an array, changes made to individual elements will affect the array if the elements are objects, but not if they are simple values like booleans. For example, if the array is [true, false]
and we use .map()
to set all elements to false
:
array.map(item => {
item = false
})
The array remains unchanged as [true, false]
.
However, if the array contains objects like
[{checked: true}, {checked: false}]
and we iterate using .map()
to update the checked
property:
array.map(item => {
item.checked = false
})
The array will be modified to
[{checked: false}, {checked: false}]
.
This illustrates the difference between iterating over arrays containing simple values versus objects.