在JavaScript中,我有一个数组,即
array = [true, false]
在某些情况下,我正在尝试初始化此数组
array.map(item => {
item = false
})
运行上面的代码后,数组不会更改,它仍然是[true, false]
所以.map
函数有时不可靠吗?
还有一个问题: 运行我的下面的代码后,数组被更改。为什么呢?
let array = [{id:1, checked: false}, {id:2, checked:true}]
array.map(item => {
item.checked = true
})
数组变为[{id:1, checked: true}, {id:2, checked:true}]
答案 0 :(得分:4)
数组映射功能正常工作。 map()方法创建一个新数组,其结果是在此数组中的每个元素上调用提供的函数。 map()不会改变调用它的数组。
You can use Github for ease
remove conflict with symbols first
======== or <<<<<<<<<
then open git shell
using command you can solve conflict coming during merge
cmd is
1 git status
2 git add then press tab button or type path of conflict file
3 again check git status
4 now color of file change from red to green
5 type cmd , git commit
6 then git push
now all set check using cmd git status
答案 1 :(得分:3)
JavaScript Array map()方法
*)创建一个新数组,其结果是为每个数组元素调用一个函数,并按顺序为数组中的每个元素调用一次提供的函数。
注意:map()方法不会为没有值的数组元素执行函数,也不会更改原始数组。
答案 2 :(得分:2)
如果你的地图很简单,你可以这样做:
var array = [true, false];
array = array.map(_ => false);
console.log(array);
// [false, false]
答案 3 :(得分:-1)
.map()
方法不可变,请查看其返回值:
var arr2 = array.map(item => false)
console.log(arr2)
在函数式编程中,它建议值不要经常更改,并且JavaScript借用了它。至少.map()
和.reduce()
这是真的。确保您了解.map()
。