改变数组里面的值的问题?

我这里现在有个数组,是[1,2],我要改成[12,24],我这么做的:但是打印后还是【1,2】,是哪里出了问题?

                 _this.opts = _[1,2];
                _this.opts.map((item,index)=>{
                  item = item*12;
                })
阅读 2.2k
2 个回答
 _this.opts = _[1,2];
 _this.opts = _this.opts.map((item,index)=>{
     return item*12;
})

or

_this.opts = _[1,2];
_this.opts = _this.opts.map(item => item*12);
)

map 接受的是返回数组内单个值的函数,而不是一个操作:

_this.opts.map(item => item * 12)

这一句之后,_this.opts 就变成 [12, 24] 了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题