Home > Web Front-end > JS Tutorial > body text

JavaScript Array扩展实现代码_javascript技巧

WBOY
Release: 2016-05-16 18:44:30
Original
1142 people have browsed it
indexOf
返回元素在数组的索引,没有则返回-1。与string的indexOf方法差不多。
如果其他浏览器没有实现此方法,可以用以下代码实现兼容:
复制代码 代码如下:

Array.prototype.indexOf = function(el, start) {
    var start = start || 0;
    for ( var i=0; i         if ( this[i] === el ) {
            return i;
        }
    }
    return -1;
};
var array = [2, 5, 9];
var index = array.indexOf(2);
// index is 0
index = array.indexOf(7);
// index is -1

lastIndexOf
与string的lastIndexOf方法差不多。
如果其他浏览器没有实现此方法,可以用以下代码实现兼容:
复制代码 代码如下:

Array.prototype.indexOf = function(el, start) {
    var start = start || 0;
    for ( var i=0; i         if ( this[i] === el ) {
            return i;
        }
    }
    return -1;
};

forEach
各类库中都实现相似的each方法。
如果其他浏览器没有实现此方法,可以用以下代码实现兼容:
复制代码 代码如下:

Array.prototype.forEach = function(fn, thisObj) {
    var scope = thisObj || window;
    for ( var i=0, j=this.length; i         fn.call(scope, this[i], i, this);
    }
};
function printElt(element, index, array) {
    print("[" + index + "] is " + element); // assumes print is already defined
}
[2, 5, 9].forEach(printElt);
// Prints:
// [0] is 2
// [1] is 5
// [2] is 9

every
如果数组中的每个元素都能通过给定的函数的测试,则返回true,反之false。换言之给定的函数也一定要返回true与false
如果其他浏览器没有实现此方法,可以用以下代码实现兼容:
复制代码 代码如下:

Array.prototype.every = function(fn, thisObj) {
    var scope = thisObj || window;
    for ( var i=0, j=this.length; i         if ( !fn.call(scope, this[i], i, this) ) {
            return false;
        }
    }
    return true;
};
function isBigEnough(element, index, array) {
  return (element }
var passed = [12, 5, 8, 130, 44].every(isBigEnough);
// passed is false
passed = [12, 54, 18, 130, 44].every(isBigEnough);
// passed is true

some
类似every函数,但只要有一个通过给定函数的测试就返回true。
如果其他浏览器没有实现此方法,可以用以下代码实现兼容:
复制代码 代码如下:

Array.prototype.some = function(fn, thisObj) {
    var scope = thisObj || window;
    for ( var i=0, j=this.length; i         if ( fn.call(scope, this[i], i, this) ) {
            return true;
        }
    }
    return false;
};
function isBigEnough(element, index, array) {
  return (element >= 10);
}
var passed = [2, 5, 8, 1, 4].some(isBigEnough);
// passed is false
passed = [12, 5, 8, 1, 4].some(isBigEnough);
// passed is true

filter
把符合条件的元素放到一个新数组中返回。
如果其他浏览器没有实现此方法,可以用以下代码实现兼容:
复制代码 代码如下:

Array.prototype.filter = function(fn, thisObj) {
    var scope = thisObj || window;
    var a = [];
    for ( var i=0, j=this.length; i         if ( !fn.call(scope, this[i], i, this) ) {
            continue;
        }
        a.push(this[i]);
    }
    return a;
};
function isBigEnough(element, index, array) {
  return (element }
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);

map
让数组中的每一个元素调用给定的函数,然后把得到的结果放到新数组中返回。。
如果其他浏览器没有实现此方法,可以用以下代码实现兼容:
复制代码 代码如下:

Array.prototype.map = function(fn, thisObj) {
    var scope = thisObj || window;
    var a = [];
    for ( var i=0, j=this.length; i         a.push(fn.call(scope, this[i], i, this));
    }
    return a;
};

var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
// roots is now [1, 2, 3]
// numbers is still [1, 4, 9]

reduce
让数组元素依次调用给定函数,最后返回一个值,换言之给定函数一定要用返回值。
如果其他浏览器没有实现此方法,可以用以下代码实现兼容:
复制代码 代码如下:

Array.prototype.reduce = function(fun /*, initial*/)
{
  var len = this.length >>> 0;
  if (typeof fun != "function")
    throw new TypeError();
  if (len == 0 && arguments.length == 1)
    throw new TypeError();
  var i = 0;
  if (arguments.length >= 2){
    var rv = arguments[1];
  } else{
    do{
      if (i in this){
        rv = this[i++];
        break;
      }
      if (++i >= len)
        throw new TypeError();
    }while (true);
  }

  for (; i     if (i in this)
      rv = fun.call(null, rv, this[i], i, this);
  }
  return rv;
};

复制代码 代码如下:

var total = [0, 1, 2, 3].reduce(function(a, b){ return a + b; });
// total == 6
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!