关于数组的一些方法

  合并两个数组,把B数组合并到A数组:

Array.prototype.push.apply(A, B);

  判断某个元素是否在数组内:

Array.prototype.contains = function ( needle ) {
   for (i in this) {
       if (this[i] == needle) return true;
   }
   return false;
}

  找出该元素在数组中的索引:

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

  误区:判断两个数组是否相等是不能用“==”直接判断的,把数组变成字符串再来比较([1,2,3].sort().toString()== [3,2,1].sort().toString())

 

posted @ 2017-09-25 14:33  红枣味酸奶  阅读(149)  评论(0编辑  收藏  举报