• JS删除数组里的某个值


    JS删除数组里的某个值

    更新2016-11-17:在stackoverflow高分回答上看到jquery之父John Resig曾经的文章写过的一个代码:

    // Array Remove - By John Resig (MIT Licensed)
    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);
    };
    

    下面给出一些实际的用例:

    // 移除数组中的第二项
    array.remove(1);
    // 移除数组中的倒数第二项
    array.remove(-2);
    // 移除数组中的第二项和第三项(从第二项开始,删除2个元素)
    array.remove(1,2);
    // 移除数组中的最后一项和倒数第二项(数组中的最后两项)
    array.remove(-2,-1);
    

    delete 删除

    • delete删除掉数组中的元素后,会把该下标出的值置为undefined,数组的长度不会变
    let arr = [2,5,4,4,6,465,45,64,54,5];
    let a = [];
    delete arr[0];
    arr.map((res)=>{
    	if(res){
    		a.push(res)
    	}
    })
    
  • 相关阅读:
    POJ:2431-Expedition
    poj:3253-Fence Repair
    POJ:3617-Best Cow Line(贪心)
    IOS各种传值方式
    imageDownloader
    Android放大镜的实现
    Delphi 串口通信(1)
    UVA 10131
    Skew Join与Left Semi Join相关
    Android动画 fillAfter和fillBefore
  • 原文地址:https://www.cnblogs.com/1748sb/p/14195062.html
Copyright © 2020-2023  润新知