前置知识 | Std.each |
复杂程度 | 中低 |
使用频率 | 中高 |
所属模块 | Array |
成员类型 | 原型扩展 |
遍历当前数组的所有元素.
方法支持2个参数,第一个参数是遍历时候执行的回调函数,每遍历一次,都会执行一次,回调函数的this对象为数组本身.
方法的第二个参数为一个Boolean值,这个值决定着遍历完成之后是否根据回调函数中的return值组成一个新的数组.
void each(Function callback);
Array each(Function callback,Boolean hasResult);
名称 | 类型 | 描述 | ||||||
callback | Function |
遍历时候的回调函数 回调参数
this对象值数组本身 |
||||||
hasResult | Boolean | 是否拥有返回值,当该值为true的时候,each方法会将回调函数中的return值放入一个新的数组中,最终返回这个数组 |
1. null
2. 新的数组
["a","b","c","d"].each(function(i,value){
return i + ":" + value;
},true);
["0:a", "1:b", "2:c", "3:d"]