资讯专栏INFORMATION COLUMN

Question - Remove empty elements from an array in

mdluo / 1203人阅读

源地址

Extending the native Array prototype Code
javascriptArray.prototype.clean = function(deleteValue) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == deleteValue) {         
      this.splice(i, 1);
      i--;
    }
  }
  return this;
};
Usage
javascripttest = new Array("","One","Two","", "Three","","Four").clean("");

test2 = [1,2,,3,,3,,,,,,4,,4,,5,,6,,,,];

test2.clean(undefined);
Pure Javascript Description

You can simply push the existing elements into other array (the example removes every "falsy" value: undefined, null, 0, false, NaN and "").

Code
javascriptfunction cleanArray(actual){
  var newArray = new Array();
  for(var i = 0; i

Usage

javascriptvar result = cleanArray([1,2,,3,,3,,,,,,4,,4,,5,,6,,,,]);
Simple Description

If you"ve got Javascript 1.6 or later you can use Array.filter using a trivial return true callback function.

Since .filter automatically skips missing elements in the original array.

The MDN page linked above also contains a nice error-checking version of filter that can be used in JavaScript interpreters that don"t support the official version.

Note that this will not remove null entries nor entries with an explicit undefined value, but the OP specifically requested "missing" entries.

Code
javascriptarr = arr.filter(function() { 
  return true; 
});
javascriptarr = arr.filter(function(n) { 
  return n != undefined;
});
javascript// only for arrays items which are numbers is numbers strings
arr = arr.filter(Number)
javascript[1, false, "", undefined, 2].filter(Boolean); // [1, 2]
javascript// only for single array items of type text
["","1","2",3,,"4",,undefined,,,"5"].join("").split("");
JQuery Code
javascriptarr = $.grep(arr, function(n){ 
  return n;
});
Underscope Code
javascript_.filter([1, false, "", undefined, 2], Boolean); // [1, 2]
javascript_.compact([1, false, "", undefined, 2]); // [1, 2]
Cool Way Description

Using ES6. IE support for filter is IE9 standards mode.

Code
javascriptvar arr  = [1,2,null, undefined,3,,3,,,0,,,4,,4,,5,,6,,,,];
var temp = [];

for(let i of arr) {
  // copy each non-empty value to the "temp" array
  i && temp.push(i); 
}

arr = temp;
delete temp; // discard the variable

arr // [1, 2, 3, 3, 4, 4, 5, 6]
Iteration Code
javascriptvar arr = [1,2,null, undefined,3,,3,,,0,,,[],,{},,5,,6,,,,];
var len = arr.length, i;

for(i = 0; i < len; i++ ) {
  // copy non-empty values to the end of the array
  arr[i] && arr.push(arr[i]);  
}

// cut the array and leave only the non-empty values
arr.splice(0 , len);  

arr // [1,2,3,3,[],Object{},5,6]

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/85424.html

相关文章

  • JAVA基础集合框架【一】ArrayList之源码翻译-上

    摘要:文章首发于基于的源码版权所有,和或其附属公司。使用须遵守许可条款。的迭代器会尽最大的努力抛出异常。因此,写程序依赖这个异常为了正确性这点是错误的,迭代器的行为仅仅被用来检查程序中的。这个类是集合框架的一员。 文章首发于:clawhub.club 基于 JDK1.8 的ArrayList源码: /* * Copyright (c) 1997, 2017, Oracle and/or...

    wean 评论0 收藏0
  • Python基础——数据类型

    摘要:本文讲解常用种数据类型通过剖析源码弄清楚每一种数据类型所有的内置函数,理解每一个函数的参数返回值使用场景是什么。 本文讲解Python常用7种数据类型:int, float, str, list, set, dict. 通过剖析源码弄清楚每一种数据类型所有的内置函数,理解每一个函数的参数、返回值、使用场景是什么。 一、整型 int Python3.6源码解析 class int(obj...

    ymyang 评论0 收藏0
  • List集合和ArrayList集合源码

    摘要:要替换的元素的索引要存储在指定位置的元素先前位于指定位置的元素返回被替换的元素如果参数指定索引,抛出一个越界异常检查索引是否越界。 文章目录 1、List集合1....

    不知名网友 评论0 收藏0
  • 容器之Collection、Iterable、List、Vector(Stack)分析(三)

    摘要:容器相关的操作及其源码分析说明本文是基于分析的。通常,我们通过迭代器来遍历集合。是接口所特有的,在接口中,通过返回一个对象。为了偷懒啊,底层使用了迭代器。即返回的和原在元素上保持一致,但不可修改。 容器相关的操作及其源码分析 说明 1、本文是基于JDK 7 分析的。JDK 8 待我工作了得好好研究下。Lambda、Stream。 2、本文会贴出大量的官方注释文档,强迫自己学英语,篇幅...

    liaosilzu2007 评论0 收藏0
  • backebone源码

    摘要: // Backbone.js 1.1.2 // (c) 2010-2014 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors // Backbone may be freely distributed under the MIT license. // For ...

    mikyou 评论0 收藏0

发表评论

0条评论

mdluo

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<