zoukankan      html  css  js  c++  java
  • Array.prototype.remove 删除数组元素

    Array.prototype.remove = function(from, to) {
        var rest = this.slice((to || from) + 1 || this.length);
        this.length = from < 0 ? this.length + from : from;
        return this.push.apply(this, rest);
    };


    这个函数扩展了JavaScript的内置对象Array,这样,我们以后的所有声明的数组都会自动的拥有remove能力,我们来看看这个方法的用法:
    var array = ["one", "two", "three", "four", "five", "six"];

    print(array);
    array.remove(0);//删除第一个元素
    print(array);
    array.remove(-1);//删除倒数第一个元素
    print(array);
    array.remove(0,2);//删除数组中下标为0-2的元素(3个)
    print(array);
    会得到这样的结果:
    one,two,three,four,five,six
    two,three,four,five,six
    two,three,four,five
    five

  • 相关阅读:
    双色球随机一注
    if else的简写
    select
    预解析
    json
    数组方法
    arguments
    国密SM4算法
    AES算法
    Feistel算法结构与DES加密算法
  • 原文地址:https://www.cnblogs.com/Mancy/p/3298810.html
Copyright © 2011-2022 走看看