Demo - 数组拓展:http://a-1.vip/demo/ele ``` var arr = [{ nome: 'abc', a: '1', b: '2', c: '1', '年龄': 22 }, { nome: 'def', a: '1', b: '1', c: '3', '年龄': 25 }, { nome: 'gh', a: '2', b: '1', c: '3', '年龄': 18 }]; // ······································································ // 遍历数组:(成员,索引值,数组自身) arr.forEach(function(ele, index, self) { console.log(this); // 打印为 [] console.log(ele, index, self); }, []); // ······················· // 遍历数组·原型、 Array.prototype.myFor = function(fun) { var _this = arguments[1] || window; // 如果传入第二的参数就改变为作用域、 for (var i = 0; i < this.length; i++) { fun.apply(_this, [this[i], i, this]); } }; // ······································································ // 数组过滤:(成员,索引值,数组自身) // 根据返回值:true、false 决定去、留; var newArr = arr.filter(function(ele, index, self) { return ele.a == 1; }); console.log(newArr); // ······················· // 数组过滤·原型、 Array.prototype.myFilter = function(fun) { var arr = []; var _this = arguments[1] || window; // 如果传入第二的参数就改变为作用域、 for (var i = 0; i < this.length; i++) { fun.apply(_this, [this[i], i, this]) && arr.push(this[i]); } return arr; }; // ······································································ // 映射数组:(成员,索引值,数组自身) 根据返回值:改变数组成员的数据、 var newArr = arr.map(function(ele, index, self) { ele.a += ' new'; // 这样会改变原数组、 return ele.a; }); // 注意:arr != newArr console.log(newArr); // ["1 new", "1 new", "2 new"] console.log(arr[0]); // Object {nome: "abc", a: "1 new", b: "2", c: "1"} // ······················· // 映射数组·原型 Array.prototype.myMap = function(fun) { var arr = [], _this = arguments[1] || window; for (var i = 0; i < this.length; i++) { arr.push(fun.apply(_this, [this[i], i, this])); } return arr; }; // ······································································ // 对一组数据(数组)进行判断,返回true/false // 所有成员都满足条件才会返回true,只要有一个成员不满足指定的条件就返回false 并终止数组的遍历。 var flag = arr.every(function(ele, index, self) { return ele['年龄'] >= 20; }); console.log(flag); //false // ······················· // 判断(筛选)数组·原型 Array.prototype.myEvery = function(fun) { var flag = true, _this = arguments[1] || window; for (var i = 0; i < this.length; i++) { if (!fun.apply(this, [this[i], i, this])) { return false; } } return flag; }; // ······································································ Array.prototype.some() -- 与Array.prototype.every()相反 // 对一组数据(数组)进行判断,返回true/false // 所有成员都满足条件才会返回false,只要有一个成员返回true,就返回true 并终止数组的遍历。 ```