The Array filter() method skips missing elements in a sparse array, and its return array is always dense. This article introduces you to the relevant knowledge of using the Array filter() method to compress sparse arrays in JavaScript. Friends who need it can refer to
What is a sparse array
Array The indexes of 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; } )
The above is what I compiled Everyone, I hope it will be helpful to everyone in the future.
Related articles:
nodejs example of method to connect to mongodb database
Select selector multi-selection verification method in iview
Using Axios Element to implement global request loading method
The above is the detailed content of Using the Array filter() method to compress sparse arrays in JavaScript. For more information, please follow other related articles on the PHP Chinese website!