Home  >  Article  >  Web Front-end  >  How to remove empty data from es6 array

How to remove empty data from es6 array

青灯夜游
青灯夜游Original
2022-10-20 17:10:223090browse

Two methods: 1. Use for to find the element with an empty value, and use splice() to delete it. The syntax is "for(i=0;i

How to remove empty data from es6 array

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

Two methods to remove empty data from es6 array

Method 1: Use for loop and splice() function

  • Use a for loop to traverse the array

  • Find the element with an empty value in the loop body

  • According to the index of the element, use the splice() function to remove the element

Implementation code:

//数组去空值
    var arr1=["某某","",3,5,"color","",3,"某某","color",""]
    console.log(arr1)
  //拓展数组去空值的方法
    Array.prototype.removeEmpty=function () {
      for(var i=0;i<this.length;i++){
        if(this[i]==""||typeof(this[i])=="undefind"){
          this.splice(i,1);
          i--;
        }
      }
      return this;
    }
    var arr2=arr1.removeEmpty();

How to remove empty data from es6 array

Method 2: Use the filter() function to filter the array and return non-empty elements

filter() function will call a callback function to filter the array elements and return all elements that meet the conditions (create a new array to return)

  • You only need to set the qualifying condition to "not empty data"

How to write the function to set the filter condition

function f(value){
	return (value);
}

Use the filter() function to call a callback function f(value) to filter the array.

Implementation code:

//数组去空值
	
function f(v){
	return (v);
}
var arr1=["某某","",3,5,"color","",3,"某某","color",""]
console.log(arr1)
var arr2=arr1.filter(f);
console.log(arr2)

How to remove empty data from es6 array

Description: filter() method

filter() method Creates a new array whose elements are checked by checking all elements in the specified array that meet the criteria.

Function: You can return the elements in the array that meet the specified conditions.

array.filter(function callbackfn(Value,index,array),thisValue)

function callbackfn(Value,index,array): A callback function, which cannot be omitted. It can accept up to three parameters:

  • value: The value of the current array element, cannot be omitted.

  • index: The numeric index of the current array element.

  • array: The array object to which the current element belongs.

The return value is a new array containing all the values ​​for which the callback function returns true. If the callback function returns false for all elements of array , the length of the new array is 0.

Let’s learn more about it through an example: Return all even numbers

var a = [2,3,4,5,6,7,8];
function f (value) {
	if (value % 2 == 0) {
		return true;
	}else{
		 return false;
	}
}
var b = a.filter(f);
console.log(b);

Output result:

How to remove empty data from es6 array

[Related recommendations: javascript Video tutorialProgramming video

The above is the detailed content of How to remove empty data from es6 array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn