map的方法

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);
}
原文地址:https://www.cnblogs.com/lucy1111/p/11232605.html