# Array.prototype.findAllIndices - for Array.prototype.findIndex

**URL:** <https://es.discourse.group/t/array-prototype-findallindices-for-array-prototype-findindex/714>\
**Category:** 🦋 Proposals\
**Created:** [March 30, 2021, 12:08pm UTC](https://es.discourse.group/t/array-prototype-findallindices-for-array-prototype-findindex/714 "2021-03-30T12:08:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaedrich](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/shaedrich/32/704_2.png) [@shaedrich](https://es.discourse.group/u/shaedrich)\
**Post date:** [March 30, 2021, 12:08pm UTC](https://es.discourse.group/t/array-prototype-findallindices-for-array-prototype-findindex/714/1 "2021-03-30T12:08:46Z")

</div>

# Situation

Different from `Set`s, array values don't have to be unique, so finding the index of a given value returns only one of n possible indecies. That's not wrong but misleading. And as we can see on mdn with `Array.prototype.indexOf` [the workaround](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexof#finding_all_the_occurrences_of_an_element) is a little messy:

```javascript
var indices = [];
var array = ['a', 'b', 'a', 'c', 'a', 'd'];
var element = 'a';
var idx = array.indexOf(element);
while (idx != -1) {
  indices.push(idx);
  idx = array.indexOf(element, idx + 1);
}
console.log(indices);
// [0, 2, 4]

```

`Array.prototype.findAll(searchValue)` on the other hand is essentially `Array.prototype.filter((value) => value === searchValue)`

# Similar pairs

- `String.prototype.match()` and `String.prototype.matchAll()`

# Similar proposals

- [`.findLast()` and `.findLastIndex()`](https://github.com/tc39/proposal-array-find-from-last)

# Aliases

- `Array.prototype.findAllIndexes()`
- `Array.prototype.findIndexAll()`

# Method signature:

Same as `Array.prototype.findIndex()` except for the return type

```javascript
/**
 * @param {(callback(element[, index[, array]]) => bool} callback
 * @return int[] - array of indices
 */
Array.prototype.findAllIndices = function(callback(element[, index[, array]])[, thisArg]) {

}

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [March 30, 2021, 3:23pm UTC](https://es.discourse.group/t/array-prototype-findallindices-for-array-prototype-findindex/714/2 "2021-03-30T15:23:04Z")

</div>

“find” always returns a single thing; it seems like you want “filterIndexes” or similar, as sugar for:

```javascript
arr.flatMap((x, i) => predicate(x) ? i : [])

```

?

---

<div class="post-metadata">

**Author:** ![shaedrich](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/shaedrich/32/704_2.png) [@shaedrich](https://es.discourse.group/u/shaedrich)\
**Post date:** [March 30, 2021, 4:18pm UTC](https://es.discourse.group/t/array-prototype-findallindices-for-array-prototype-findindex/714/3 "2021-03-30T16:18:23Z")

</div>

Um, probably …

---

<div class="post-metadata">

**Author:** ![Rudxain](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rudxain/32/2272_2.png) [@Rudxain](https://es.discourse.group/u/Rudxain)\
**Post date:** [November 11, 2022, 10:20pm UTC](https://es.discourse.group/t/array-prototype-findallindices-for-array-prototype-findindex/714/4 "2022-11-11T22:20:17Z")

</div>

I think names like `findIndices` and `filterIndices` are more consistent with current naming conventions.

We could also have a `findEntry`, `findLastEntry`, and `findEntries`, which do the same as `find`, `findLast`, and `filter` (respectively), but return an index paired with its corresponding value (as a length=2 Array).

Names inspired by `Object.entries`
