Question - Remove empty elements from an array in Javascript

发布时间:2019-08-20 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Question - Remove empty elements from an array in Javascript脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

源地址

Extending the native Array prototype

Code

Array.PRototype.clean = function(deleteValue) {
  for (VAR i = 0; i < this.length; i++) {
    if (this[i] == deleteValue) {         
      this.splice(i, 1);
      i--;
    }
  }
  return this;
};

usage

test = 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

function cleanArray(actual){
  var newArray = new Array();
  for(var i = 0; i<actual.length; i++){
    if (actual[i]){
      newArray.push(actual[i]);
    }
  }
  return newArray;
}

Usage

var 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

arr = arr.filter(function() { 
  return true; 
});
arr = arr.filter(function(n) { 
  return n != undefined;
});
// only for arrays items @R_126_1380@ are numbers is numbers strings
arr = arr.filter(Number)
[1, false, "", undefined, 2].filter(Boolean); // [1, 2]
// only for single array items of type text
['','1','2',3,,'4',,undefined,,,'5'].join('').split('');

jquery

Code

arr = $.grep(arr, function(n){ 
  return n;
});

Underscope

Code

_.filter([1, false, "", undefined, 2], Boolean); // [1, 2]
_.COMpact([1, false, "", undefined, 2]); // [1, 2]

Cool Way

Description

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

Code

var 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

var 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]

脚本宝典总结

以上是脚本宝典为你收集整理的Question - Remove empty elements from an array in Javascript全部内容,希望文章能够帮你解决Question - Remove empty elements from an array in Javascript所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。