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

  • 相关阅读:
    编辑语言发展历史
    正则表达式
    css
    伪类和伪元素区别
    WEB 之API端口
    event flow (Dow)
    for衍生对象
    前端语言的发展
    document
    password user message email的正则表达式
  • 原文地址:https://www.cnblogs.com/Mancy/p/3298810.html
Copyright © 2011-2022 走看看