Use js to find the specified Element in the array and return all index examples of the element. js Array
Preface
This article mainly introduces how to use js to find the specified Element in the array and return all the indexes of the element. The detailed sample code is provided in this Article, let's take a look at the detailed sample code.
Sample Code
// Search for all the existing x in the array and return an Array function findall (a, x) {var results = [], len =. length, pos = 0; while (pos <len) {pos =. indexOf (x, pos); if (pos =-1) {// exit the loop to complete the break search if not found;} results. push (pos); // store the index pos + = 1; // search from the next position} return results;} var arr = [1, 2, 3, 1, 4, 1, 4, 1]; findall (arr, 1); // return [,]
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.