# String.prototype.entries()

**URL:** <https://es.discourse.group/t/string-prototype-entries/1883>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [December 26, 2023, 2:24pm UTC](https://es.discourse.group/t/string-prototype-entries/1883 "2023-12-26T14:24:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfulton26](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/mfulton26/32/578_2.png) [@mfulton26](https://es.discourse.group/u/mfulton26)\
**Post date:** [December 26, 2023, 2:24pm UTC](https://es.discourse.group/t/string-prototype-entries/1883/1 "2023-12-26T14:24:27Z")

</div>

We have the following:

1. `Array.prototype.entries()`
2. `Map.prototype.entries()`
3. `Set.prototype.entries()`
4. `TypedArray.prototype.entries()`

We don't yet have `String.prototype.entries()`.

With `Array.prototype.entries()` I can write:

```js
for (const [y, row] of grid.entries()) {
  for (const [x, value] of row.entries()) {
    /* handle x, y, value tuple in a "2D" array */
  }
}

```

If instead I have an array of strings then I'd like to be able to write the same code as for an array of arrays but where the inner for loop iterates over the code points of the string (consistent with `String.prototype[Symbol.iterator]()`) along with the index.

---

<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:** [December 26, 2023, 4:02pm UTC](https://es.discourse.group/t/string-prototype-entries/1883/2 "2023-12-26T16:02:11Z")

</div>

That would mean the index doesn’t line up with the code units of the string (since it’d be iterating code points), which might be confusing. What’s the use case?

---

<div class="post-metadata">

**Author:** ![dmchurch](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/dmchurch/32/1962_2.png) [@dmchurch](https://es.discourse.group/u/dmchurch)\
**Post date:** [December 26, 2023, 7:32pm UTC](https://es.discourse.group/t/string-prototype-entries/1883/3 "2023-12-26T19:32:47Z")

</div>

Who says the index needs to increase by 1 each time?

---

<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:** [December 26, 2023, 7:47pm UTC](https://es.discourse.group/t/string-prototype-entries/1883/4 "2023-12-26T19:47:20Z")

</div>

True, but then the index isn't matching up to each iteration.

I'm still interested in the use case, especially considering iterator helpers now allows you to do `Iterator.from(string).map((codePoint, count) => {})`.

---

<div class="post-metadata">

**Author:** ![dmchurch](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/dmchurch/32/1962_2.png) [@dmchurch](https://es.discourse.group/u/dmchurch)\
**Post date:** [December 26, 2023, 7:55pm UTC](https://es.discourse.group/t/string-prototype-entries/1883/5 "2023-12-26T19:55:26Z")

</div>

> [@ljharb](#):
>
> True, but then the index isn't matching up to each iteration.

The .entries() contract doesn't guarantee that - see Object.entries. What it _does_ guarantee is that every key returned can be used to access the associated value on the original object.

And I'd say that iterator helper - which, as your choice of parameter naming shows, returns the count of values and not their index - is a perfect reason to have String.prototype.entries() return something else. Maybe I want to iterate through the characters of a string to find the start of the substring I'm looking for, then slice() the string from that point? I need the code unit index for that.

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [January 2, 2024, 9:11pm UTC](https://es.discourse.group/t/string-prototype-entries/1883/6 "2024-01-02T21:11:07Z")

</div>

Yeah, this is definitely a missing feature.

---

<div class="post-metadata">

**Author:** ![mfulton26](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/mfulton26/32/578_2.png) [@mfulton26](https://es.discourse.group/u/mfulton26)\
**Post date:** [April 20, 2024, 1:57pm UTC](https://es.discourse.group/t/string-prototype-entries/1883/7 "2024-04-20T13:57:46Z")

</div>

What might be good next steps here? I've read/seen various TC39 proposals over the years but I've never authored one… I'm concerned that might be a lot of work but I suppose in order to get anything added folks need to champion them.

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [April 22, 2024, 8:07am UTC](https://es.discourse.group/t/string-prototype-entries/1883/8 "2024-04-22T08:07:18Z")

</div>

Hi @mfulton26!

Yep, a [TC39 member](https://github.com/tc39/how-we-work/blob/2681abd861e7ced9411bb1cf2ac675a9e89e7d81/join-tc39.md) is required to champion the proposal: [ecma262/CONTRIBUTING.md at 5d8f62d6ab761960206c3f6ddf4992534fe9726f · tc39/ecma262 · GitHub](https://github.com/tc39/ecma262/blob/5d8f62d6ab761960206c3f6ddf4992534fe9726f/CONTRIBUTING.md#tc39-meetings-and-champions)
