成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Question - Remove empty elements from an array in

mdluo / 1320人閱讀

源地址

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]

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/85424.html

相關(guān)文章

  • JAVA基礎(chǔ)集合框架【一】ArrayList之源碼翻譯-上

    摘要:文章首發(fā)于基于的源碼版權(quán)所有,和或其附屬公司。使用須遵守許可條款。的迭代器會(huì)盡最大的努力拋出異常。因此,寫程序依賴這個(gè)異常為了正確性這點(diǎn)是錯(cuò)誤的,迭代器的行為僅僅被用來(lái)檢查程序中的。這個(gè)類是集合框架的一員。 文章首發(fā)于:clawhub.club 基于 JDK1.8 的ArrayList源碼: /* * Copyright (c) 1997, 2017, Oracle and/or...

    wean 評(píng)論0 收藏0
  • Python基礎(chǔ)——數(shù)據(jù)類型

    摘要:本文講解常用種數(shù)據(jù)類型通過(guò)剖析源碼弄清楚每一種數(shù)據(jù)類型所有的內(nèi)置函數(shù),理解每一個(gè)函數(shù)的參數(shù)返回值使用場(chǎng)景是什么。 本文講解Python常用7種數(shù)據(jù)類型:int, float, str, list, set, dict. 通過(guò)剖析源碼弄清楚每一種數(shù)據(jù)類型所有的內(nèi)置函數(shù),理解每一個(gè)函數(shù)的參數(shù)、返回值、使用場(chǎng)景是什么。 一、整型 int Python3.6源碼解析 class int(obj...

    ymyang 評(píng)論0 收藏0
  • 容器之Collection、Iterable、List、Vector(Stack)分析(三)

    摘要:容器相關(guān)的操作及其源碼分析說(shuō)明本文是基于分析的。通常,我們通過(guò)迭代器來(lái)遍歷集合。是接口所特有的,在接口中,通過(guò)返回一個(gè)對(duì)象。為了偷懶啊,底層使用了迭代器。即返回的和原在元素上保持一致,但不可修改。 容器相關(guān)的操作及其源碼分析 說(shuō)明 1、本文是基于JDK 7 分析的。JDK 8 待我工作了得好好研究下。Lambda、Stream。 2、本文會(huì)貼出大量的官方注釋文檔,強(qiáng)迫自己學(xué)英語(yǔ),篇幅...

    liaosilzu2007 評(píng)論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 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<