Home  >  Article  >  Web Front-end  >  How to use the Array filter() method in JS

How to use the Array filter() method in JS

php中世界最好的语言
php中世界最好的语言Original
2018-03-17 13:40:391844browse

This time I will show you how to use the Array filter() method in JS. What are the precautions for using the Array filter() method in JS. The following is a practical case. Let’s take a look. .

What is a sparse array

The indexes of array elements do not have to be consecutive, there can be gaps between them. Every

javaScript array has a length property. For non-sparse arrays, this property is the number of array elements; for sparse arrays, length is greater than the number of all elements.

The Array filter() method skips missing elements in a sparse array, and its return array is always dense.

(1) Compress the vacancies of the sparse array:

var dense = sparse.filter( function(currentValue)
{ 
return true; 
}
);
(2) Compress the vacancies of the sparse array, and

delete undefined and null elements:

var dense = sparse.filter( function(currentValue) 
{ 
return currentValue !== undefined && currentValue!= null;
} 
)

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

D3.js draws dynamic progress bar

##ngrok+express for local environment WeChat interface debugging


Custom settings for Bootstrap4

The above is the detailed content of How to use the Array filter() method in JS. 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