码迷,mamicode.com
首页 > 其他好文 > 详细

map的方法

时间:2019-07-23 16:41:35      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:bsp   color   array   style   示例   javascrip   否则   使用   slice   

JavaScript Array map() 方法

map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。

map() 方法按照原始数组元素顺序依次处理元素。

注意: map() 不会对空数组进行检测。

注意: map() 不会改变原始数组。

 

示例:数组this.$parent.gradeList需要取出其中的id返回新数组this.gradeId

this.gradeId = this.$parent.gradeList.slice(0).map(d => d.id);
 
注意这里使用了slice(0),由于vue指向的问题,需要返回一个新数组来处理,否则this.gradeId是指向  this.$parent.gradeList会导致逻辑错误
 
等价于
 
for (var k = 0; k < this.$parent.gradeList.length; k++) {
  this.gradeId.push(this.$parent.gradeList[k].id);
}

map的方法

标签:bsp   color   array   style   示例   javascrip   否则   使用   slice   

原文地址:https://www.cnblogs.com/lucy1111/p/11232605.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!