作者:逝去成回忆2502920253 | 来源:互联网 | 2023-06-05 10:58
相同点
都是循环遍历数组中的每一项
forEach和map方法里每次执行匿名函数都支持3个参数,参数分别是item(当前每一项)、index(索引值)、arr(原数组)
匿名函数中的this
相同点
- 都是循环遍历数组中的每一项
- forEach和map方法里每次执行匿名函数都支持3个参数,参数分别是item(当前每一项)、index(索引值)、arr(原数组)
- 匿名函数中的this都是指向window
- 只能遍历数组
forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数。
注意: forEach() 对于空数组是不会执行回调函数的。
forEach没有返回值,所以不能链式调用
语法
array.forEach(function(currentValue, index, arr), thisValue)
currentValue // 代表当前元素
index // 代表当前元素的索引
arr // 当前元素所属的数组对象
案例:
Map
map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。
map() 方法按照原始数组元素顺序依次处理元素。
注意: map() 不会对空数组进行检测。
注意: map() 不会改变原始数组。。
语法
array.map(function(currentValue,index,arr), thisValue)
currentValue // 代表当前元素
index // 代表当前元素的索引
arr // 当前元素所属的数组对象
eg
// 这里是 filter() map() join() 连用
// eg
let storeOutSUMs = this.outOrderTable.data.filter(
(filter) => filter.storeOutSUM == 0
);
// console.log(storeOutSUMs);
if (storeOutSUMs.length > 0) {
this.$Message.error(
`号码为${storeOutSUMs
.map((m) => m.itemInfo_ItemCode)
.join(",")}的值为0!无法使用!`
);
return;
}
// eg