forked from davidflanagan/jstdg7
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindall.js
More file actions
14 lines (14 loc) · 716 Bytes
/
findall.js
File metadata and controls
14 lines (14 loc) · 716 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Find all occurrences of a value x in an array a and return an array
// of matching indexes
function findall(a, x) {
let results = [], // The array of indexes we'll return
len = a.length, // The length of the array to be searched
pos = 0; // The position to search from
while(pos < len) { // While more elements to search...
pos = a.indexOf(x, pos); // Search
if (pos === -1) break; // If nothing found, we're done.
results.push(pos); // Otherwise, store index in array
pos = pos + 1; // And start next search at next element
}
return results; // Return array of indexes
}