# A TC39 proposal to add iterator-like methods to objects

**URL:** https://es.discourse.group/t/a-tc39-proposal-to-add-iterator-like-methods-to-objects/1942
**Category:** 🦋 Proposals
**Tags:** proposal
**Created:** [February 26, 2024, 7:50am UTC](https://es.discourse.group/t/a-tc39-proposal-to-add-iterator-like-methods-to-objects/1942 "2024-02-26T07:50:14Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Ayc0](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ayc0/32/424_2.png) [@Ayc0](https://es.discourse.group/u/Ayc0)
#### Post date: [February 26, 2024, 7:50am UTC](https://es.discourse.group/t/a-tc39-proposal-to-add-iterator-like-methods-to-objects/1942/1 "2024-02-26T07:50:14Z")

</div>

Currently in ECMAScript, iterating over & manipulating `Object`s is also a bit tedious at the moment in ECMAScript. We need to use `Object.keys(obj)`, `Object.values(obj)`, or `Object.entries(obj)` and chain them. So if we want to create a new object from an previous one with the same keys, only a modification of its values, we need to write:

```js
Object.fromEntries(Object.entries(object).map(([key, value]) => [key, transformation(value)])

```

This is a bit verbose.

Manipulating objects could be made simpler.

## Proposal [GitHub - Ayc0/proposal-object-iterator: A TC39 proposal to add iterator-like methods to objects](https://github.com/Ayc0/proposal-object-iterator)

---

<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: [February 26, 2024, 9:32am UTC](https://es.discourse.group/t/a-tc39-proposal-to-add-iterator-like-methods-to-objects/1942/2 "2024-02-26T09:32:34Z")

</div>

Adding new methods to `Object.prototype` is unlikely to be web compatiable. For example a website could be doing:

```javascript
if (config.forEach) {
  // assumes config is array
} else {
  // assumes config is plain object
}

```

This isn't only the case for `Object`, it happens with `Array` too, e.g. the `Array.prototype.group` proposal turned out to not be web compatible, so was changed to `Object.groupBy` (static method). Because almost all objects inherit from `Object.prototype` the risk factor is increased.

---

<div class="post-metadata">

### Author: ![Ayc0](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ayc0/32/424_2.png) [@Ayc0](https://es.discourse.group/u/Ayc0)
#### Post date: [February 26, 2024, 9:54am UTC](https://es.discourse.group/t/a-tc39-proposal-to-add-iterator-like-methods-to-objects/1942/3 "2024-02-26T09:54:47Z")

</div>

what about using `Object.map`?

But then it'll conflict with MooTools 😕

Edit: after checking the library, it should be fine

---

<div class="post-metadata">

### Author: ![senocular](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/senocular/32/642_2.png) [@senocular](https://es.discourse.group/u/senocular)
#### Post date: [February 26, 2024, 1:15pm UTC](https://es.discourse.group/t/a-tc39-proposal-to-add-iterator-like-methods-to-objects/1942/4 "2024-02-26T13:15:44Z")

</div>

Something like this has been proposed before and withdrawn:

> **[GitHub - tc39/proposal-object-iteration: ECMA TC39 proposal for making...](https://github.com/tc39/proposal-object-iteration)**
>
> ECMA TC39 proposal for making mapping over Objects more concise - tc39/proposal-object-iteration

Looks like the history section doesn't include the Feb 2020 notes that seems to have caused the withdrawal, but you can find that here (I didn't read myself but there should be more info - click the link for the full notes rather than relying on the preview here):

> <https://github.com/tc39/notes/blob/22e8269694c7bdf030c851e58125a920dc54bab1/meetings/2020-02/february-5.md#object-iteration-for-stage-2>

---

<div class="post-metadata">

### Author: ![Ayc0](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ayc0/32/424_2.png) [@Ayc0](https://es.discourse.group/u/Ayc0)
#### Post date: [February 26, 2024, 5:01pm UTC](https://es.discourse.group/t/a-tc39-proposal-to-add-iterator-like-methods-to-objects/1942/5 "2024-02-26T17:01:00Z")

</div>

I'm gonna drop the iterator from this proposal, and move the functions to `Object.<function>`. After checking MooTools, it should be compliant

This will avoid the issue with the prototype chain

---

<div class="post-metadata">

### Author: ![Ayc0](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ayc0/32/424_2.png) [@Ayc0](https://es.discourse.group/u/Ayc0)
#### Post date: [February 26, 2024, 5:19pm UTC](https://es.discourse.group/t/a-tc39-proposal-to-add-iterator-like-methods-to-objects/1942/6 "2024-02-26T17:19:27Z")

</div>

@senocular tbh, I was hesitant about adding the paragraph about making object iterables (I mentioned "could be split in 2 proposals"), and same about modifying the prototype.  
I didn't fully think about all issues it could introduce, from the `'map' in …`, or also the fact that it'd add for instance `.map` on `WeakSet` which we **don’t want at all**.

But adding `Object.map(object, fn)` that would be sort of an alias for `Object.fromEntries(Object.entries(object).map(([key, value]) => [key, fn(value)]))` shouldn’t have the issues that were mentioned in the previous meeting note, right?

As mentioned in my previous message, I checked MooTools. And according to their docs, their `Object.map` behaves exactly like the one I'm proposing in this proposal (and same for the other methods).

---

<div class="post-metadata">

### Author: ![Ayc0](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ayc0/32/424_2.png) [@Ayc0](https://es.discourse.group/u/Ayc0)
#### Post date: [February 26, 2024, 8:44pm UTC](https://es.discourse.group/t/a-tc39-proposal-to-add-iterator-like-methods-to-objects/1942/7 "2024-02-26T20:44:15Z")

</div>

The proposal was updated:

- I dropped the mention of making object iterable,
- I updated the functions to make them static methods on `Object` instead of functions on the prototype,
- I added a new section in the QnA
