Home > Article > Web Front-end > How to use the Array filter() method in JS
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 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
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!