Home > Web Front-end > JS Tutorial > body text

How to use the Array filter() method in JS

php中世界最好的语言
Release: 2018-03-17 13:40:39
Original
1842 people have browsed it

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; 
}
);
Copy after login
(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;
} 
)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template