数组贴图功能不会更改元素 [英] Array map function doesn't change elements

查看:66
本文介绍了数组贴图功能不会更改元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,我有一个数组,

In JavaScript, I have an array, which is

array = [true, false]

在某些情况下,我正在尝试初始化此数组

In some cases, I am trying to initialize this array

array.map(item => {
   item = false
})

运行上面的代码后,数组没有更改,仍然是[true, false],所以.map有时不可靠吗?

After running the above code, the array is not changed, it is still [true, false], so is .map not reliable sometimes?

一个更多的问题: 运行下面的代码后,将更改数组.为什么在这种情况下有效?

ONE MORE QUESTION: After running my below code, the array is changed. Why does it work in this case?

let array = [{id:1, checked: false}, {id:2, checked:true}]
array.map(item => {
    item.checked = true
})

数组变为[{id:1, checked: true}, {id:2, checked:true}]

推荐答案

JavaScript Array map()方法

JavaScript Array map() Method

*)创建一个新数组,其结果是为每个数组元素调用一个函数,并依次为数组中的每个元素调用提供的函数一次.

*)creates a new array with the results of calling a function for every array element and it calls the provided function once for each element in an array, in order.

注意:map()方法不会对没有值的数组元素执行函数,并且不会更改原始数组.

Note: map() Method does not execute the function for array elements without values and it does not change the original array.

更多详细信息

这篇关于数组贴图功能不会更改元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆