做前端有多年了,看过不少技术文章,学了新的技术,但更新迭代快的大前端,庞大的知识库,很多学过就忘记了,特别在项目紧急的条件下,哪怕心中隐隐约约有学过一个方法,但会下意识的使用旧的方法去解决,多年前ES5几个新增的数组方法,好用但是常忘记用,趁着这周比较清闲,重温下并做下笔记,养成记笔记的好习惯. forEach map filter some every reduce reduceRight forEach forEach是ES5的Array方法中用得最频繁的一个,就是遍历,循环输出,它接受一个…
Array.prototype.map = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); var res = new Array(len); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) res[i] = fun.call…
ES6新增的常用数组方法 let arr = [1, 2, 3, 2, 1]; 一 forEach => 遍历数组 arr.forEach((v, i) => { console.log(v, i); }); 二 map => 使用一个数组, 利用某规则映射得到一个新数组 let mapArr = arr.map((v, i) => { return v * v; }); arr.map((v, i) => v * v); // 如果只有一句话, 可以省略大括号和return…
1. forEach const colors = ['red', 'blue', 'green'] colors.forEach(function (params) { console.log(params) }) 2. map  重构-返回数组 const porducts = [ { name: 'cucumber', type: 'vegetable', quantity: 0, price: 1 }, { name: 'cucumber', type: 'vegetable', qua…
ES5中,一共有9个Array方法 Array.prototype.indexOf Array.prototype.lastIndexOf Array.prototype.every Array.prototype.some Array.prototype.forEach Array.prototype.map Array.prototype.filter Array.prototype.reduce Array.prototype.reduceRight…
1:  forEacharray.forEach(callback,[ thisObject]) // 遍历数组里面的所有数字// item 是值, i 是序号, array 是整个数组 [1, 2 ,3, 4].forEach(function(item, i, array){ /* 0 1 [1, 2, 3, 4] 1 2 [1, 2, 3, 4] 2 3 [1, 2, 3, 4] 3 4 [1, 2, 3, 4] */ console.log(i, item, array); }); 第2…
ECMAScript 3在Array.prototype中定义了一些很有用的操作数组的函数,这意味着这些函数作为任何数组的方法都是可用的. 1.Array.join()方法 Array.join()方法将数组中所有元素都转化为字符串并连接在一起,返回最后生成的字符串.可以指定一个可选的符号或字符串在生成的字符串中来分隔数组的各个元素.如果不指定分隔符,默认使用逗号.注意:此方法不会改变原始数组 var arr = ['a', 'b', 'c']; console.log(arr.join());…
原文中部分源码来源于:JS Array.reduce 实现 Array.map 和 Array.filter Array 中的高阶函数 ---- map, filter, reduce map() - 映射 var newArr = array.map((currentValue, index, array) => { return ... }, thisValue); currentValue, 必须,当前的元素值: index, 可选,当前元素值的索引: array, 可选,原数组: thi…
forEach  some  filter  findIndex这些都属于数组的新方法,都会对数组中的每一项,进行遍历,执行相关的操作: 只不过在循环的时候有些不一样 参考资料:https://wangdoc.com/javascript/stdlib/array.html#filter…
处理数组的forEach //forEach处理 if(!Array.prototype.forEach) { Array.prototype.forEach = function (callback) { for(var i=0,len=this.length;i<len;i++) { callback(this[i], i); } } } 处理数组的map //处理map if(!Array.prototype.map) { Array.prototype.map = function (c…