indexOf()、forEach()、map()、filter()、some()、every()、find()、findIndex()、copyWithin()、includes()、reduce() 等兼容性写法汇总。
indexOf()
indexOf()方法 返回根据给定元素找到的第一个索引值,否则返回-1。
- if (!Array.prototype.indexOf) {
- Array.prototype.indexOf = function(ele) {
-
- var len = this.length;
-
- var fromIndex = Number(arguments[1]) || 0;
-
- if (fromIndex < 0) {
- fromIndex += len;
- }
-
- while (fromIndex < len) {
-
- if (fromIndex in this && this[fromIndex] === ele) {
- return fromIndex;
- }
- fromIndex++;
- }
-
- if (len === 0) {
- return -1;
- }
- };
- }
forEach()
forEach() 方法让数组的每一项都执行一次给定的函数。forEach() 方法会修改原数组。
- if (!Array.prototype.forEach) {
- Array.prototype.forEach = function forEach(callback) {
-
- var len = this.length;
- if (typeof callback != "function") {
- throw new TypeError();
- }
-
- var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
- for (var i = 0; i < len; i++) {
- if (i in this) {
-
- callback.call(thisArg, this[i], i, this);
- }
- }
- };
- }
map()
map() 方法返回一个由原数组中的每个元素调用一个指定方法后的返回值组成的新数组。map() 被调用时不会改变数组。
- if (!Array.prototype.map) {
- Array.prototype.map = function(callback) {
-
- var len = this.length;
- if (typeof callback != "function") {
- throw new TypeError();
- }
-
- var newArr = new Array(len);
-
- var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
- for (var i = 0; i < len; i++) {
- if (i in this) {
- newArr[i] = callback.call(thisArg, this[i], i, this);
- }
- }
- return newArr;
- };
- }
filter()
filter() 方法利用所有通过指定函数测试的元素创建一个新的数组,并返回。filter() 被调用时不会改变数组。
- if (!Array.prototype.filter) {
- Array.prototype.filter = function(callback) {
-
- var len = this.length;
- if (typeof callback != "function") {
- throw new TypeError();
- }
-
- var newArr = new Array();
-
- var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
- for (var i = 0; i < len; i++) {
- if (i in this) {
- if (callback.call(thisArg, this[i], i, this)) {
- newArr.push(val);
- }
- }
- }
- return newArr;
- };
- }
some()
some() 方法测试数组中的某些元素是否通过了指定函数的测试。返回布尔值。some() 被调用时不会改变数组。
- if (!Array.prototype.some) {
- Array.prototype.some = function(callback) {
- // 获取数组长度
- var len = this.length;
- if (typeof callback != "function") {
- throw new TypeError();
- }
- // thisArg为callback 函数的执行上下文环境
- var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
- for (var i = 0; i < len; i++) {
- if (i in this && callback.call(thisArg, this[i], i, this)) {
- return true;
- }
- }
- return false;
- };
- }
every()
every() 方法测试数组的所有元素是否都通过了指定函数的测试。every() 不会改变原数组。
- if (!Array.prototype.every) {
- Array.prototype.every = function(callback) {
- // 获取数组长度
- var len = this.length;
- if (typeof callback != "function") {
- throw new TypeError();
- }
- // thisArg为callback 函数的执行上下文环境
- var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
- for (var i = 0; i < len; i++) {
- if (i in this && !callback.call(thisArg, this[i], i, this)) {
- return false;
- }
- }
- return true;
- };
- }
find()
find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined。
- if (!Array.prototype.find) {
- Array.prototype.find = function(callback) {
- if (this == null) {
- throw new TypeError('"this" is null or not defined');
- }
- if (typeof callback != "function") {
- throw new TypeError();
- }
- var o = Object(this);
- var len = o.length >>> 0;
- var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
- var k = 0;
- while (k < len) {
- var kValue = o[k];
- if (callback.call(thisArg, kValue, k, o)) {
- return kValue;
- }
- k++;
- }
- return undefined;
- };
- }
findIndex()
findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1。
- if (!Array.prototype.findIndex) {
- Array.prototype.findIndex = function(callback) {
- if (this == null) {
- throw new TypeError('"this" is null or not defined');
- }
- if (typeof callback != "function") {
- throw new TypeError();
- }
- var o = Object(this);
- var len = o.length >>> 0;
- var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
- var k = 0;
- while (k < len) {
- var kValue = o[k];
- if (callback.call(thisArg, kValue, k, o)) {
- return k;
- }
- k++;
- }
- return -1;
- };
- }
copyWithin()
copyWithin() 方法浅复制数组的一部分到同一数组中的另一个位置,并返回它,而不修改其大小。
- if (!Array.prototype.copyWithin) {
- Array.prototype.copyWithin = function(target, start ) {
- if (this == null) {
- throw new TypeError('this is null or not defined');
- }
- var O = Object(this);
- var len = O.length >>> 0;
- var relativeTarget = target >> 0;
- var to = relativeTarget < 0 ? Math.max(len + relativeTarget, 0) : Math.min(relativeTarget, len);
- var relativeStart = start >> 0;
- var from = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len);
- var end = arguments[2];
- var relativeEnd = end === undefined ? len : end >> 0;
- var final = relativeEnd < 0 ? Math.max(len + relativeEnd, 0) : Math.min(relativeEnd, len);
- var count = Math.min(final - from, len - to);
- var direction = 1;
- if (from < to && to < (from + count)) {
- direction = -1;
- from += count - 1;
- to += count - 1;
- }
- while (count > 0) {
- if (from in O) {
- O[to] = O[from];
- } else {
- delete O[to];
- }
- from += direction;
- to += direction;
- count--;
- }
- return O;
- };
- }
includes()
includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false。
- if (!Array.prototype.includes) {
- Array.prototype.includes = function(searchElement, fromIndex) {
- if (this == null) {
- throw new TypeError('"this" is null or not defined');
- }
- var o = Object(this);
- var len = o.length >>> 0;
- if (len === 0) {
- return false;
- }
- var n = fromIndex | 0;
- var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
- while (k < len) {
- if (o[k] === searchElement) {
- return true;
- }
- k++;
- }
- return false;
- };
- }
reduce()
reduce() 方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值。
- if (!Array.prototype.reduce) {
- Array.prototype.reduce = function(callback ) {
- if (this == null) {
- throw new TypeError('"this" is null or not defined');
- }
- if (typeof callback != "function") {
- throw new TypeError();
- }
- var o = Object(this);
- var len = o.length >>> 0;
- var k = 0;
- var value;
- if (arguments.length >= 2) {
- value = arguments[1];
- } else {
- while (k < len && !(k in o)) {
- k++;
- }
- if (k >= len) {
- throw new TypeError('Reduce of empty array ' + 'with no initial value');
- }
- value = o[k++];
- }
- while (k < len) {
- if (k in o) {
- value = callback(value, o[k], k, o);
- }
- k++;
- }
- return value;
- };
- }
评论 (0)