zoukankan      html  css  js  c++  java
  • Array原生方法

    push()末尾推入元素 返回数组长度

    pop()末尾弹出元素 返回弹出元素

    shift()起始弹出元素 返回弹出元素

    unshift()起始推入元素 返回数组长度

    代码如:

    var arr1 = ["b","c","d","e"];

    var arr2 = arr1.push("f");//arr1:["b","c","d","e","f"], arr2:5

    var arr3 = arr1.pop();//arr1:["b","c","d","e"], arr3:"f"

    var arr4 = arr1.unshift("a");//arr1:["a","b","c","d","e"], arr4:5

    var arr5 = arr1.shift();//arr1:["b","c","d","e"], arr5:"a"

    Ecmascript5有扩展数组原型方法forEach,filter等

    if(typeof Array.prototype.forEach !== "function"){

      Array.prototype.forEach = function(fn,thisObj){

          var scope = thisObj || window;

          for(var i=0, len=this.length; i<len; i++){

            fn.call(scope,this[i],[i],this);

          }

      }

    };

    if(typeof Array.prototype.filter!== "function"){

      Array.prototype.filter= function(fn,thisObj){

          var scope = thisObj || window;

          var a = [];

          for(var i=0, len=this.length; i<len; i++){

            if(!fn.call(scope,this[i],[i],this)){

              continue;

            }

            a.push(this[i])

          }

          return a;

      }

    }

  • 相关阅读:
    Day2-Python爬虫小练 爬取百科词条
    Day1-python轻量级爬虫
    大数据处理课堂测试1
    周记7-28
    周记7-21
    周记7-14
    软件工程课程总结
    进度15
    NABCD
    团队项目成员和题目
  • 原文地址:https://www.cnblogs.com/samKR/p/3794493.html
Copyright © 2011-2022 走看看