# Map.prototype.addEntries(iterable)

**URL:** <https://es.discourse.group/t/map-prototype-addentries-iterable/361>\
**Category:** 🦋 Proposals\
**Tags:** proposal\
**Created:** [June 16, 2020, 3:19am UTC](https://es.discourse.group/t/map-prototype-addentries-iterable/361 "2020-06-16T03:19:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nanaze](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nanaze/32/345_2.png) [@nanaze](https://es.discourse.group/u/nanaze)\
**Post date:** [June 16, 2020, 3:19am UTC](https://es.discourse.group/t/map-prototype-addentries-iterable/361/1 "2020-06-16T03:19:53Z")

</div>

Both Java and Python (and others, surely) have a method on maps to add entries ([putAll](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#putAll-java.util.Map-) and [update](https://docs.python.org/3/library/stdtypes.html#dict.update)).

`Map` permits an iterable of entries to the constructor, but has no way to simply add an iterable of entries to an existing map. The current typical code is:

```javascript
mapA.forEach((val, key) => {mapB.set(key, val)});

```

or

```javascript
for (const [key, val] of mapA) {
  mapB.set(key, val);
}

```

Rather, I'd expect to be able to do the following, as I would in Java or Python:

```javascript
mapA.addEntries(<iterable>)

```

where `<iterable>` is another map, the result of Object.prototype.entries(), or anything else that gives 2-value tuples of key-value pairs.

The best method name is up for debate, but contenders would be `addEntries`, `extend`, `addAll`.

---

<div class="post-metadata">

**Author:** ![nanaze](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nanaze/32/345_2.png) [@nanaze](https://es.discourse.group/u/nanaze)\
**Post date:** [June 16, 2020, 4:08am UTC](https://es.discourse.group/t/map-prototype-addentries-iterable/361/2 "2020-06-16T04:08:22Z")

</div>

My friend Brad [made a good point](https://twitter.com/bradfitz/status/1272739567662018565) -- such a method would likely allow a performance improvement over the current iterate-and-set solution (in addition to being more concise and understandable).

---

<div class="post-metadata">

**Author:** ![bathos](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bathos/32/76_2.png) [@bathos](https://es.discourse.group/u/bathos)\
**Post date:** [June 16, 2020, 4:55am UTC](https://es.discourse.group/t/map-prototype-addentries-iterable/361/3 "2020-06-16T04:55:20Z")

</div>

I’ve wanted this many times. Seems like it would be an uncontroversial addition to me.

I’d expect a corresponding update to Set. There is an active Set prototype methods proposal, but I don’t think there’d be overlap, because Set.prototype.union is for creating a new Set, rather than for adding items to the receiver set.

---

<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:** [June 16, 2020, 4:57am UTC](https://es.discourse.group/t/map-prototype-addentries-iterable/361/4 "2020-06-16T04:57:42Z")

</div>

You may want to suggest this in [this proposal](https://github.com/tc39/proposal-collection-methods).

---

<div class="post-metadata">

**Author:** ![nanaze](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nanaze/32/345_2.png) [@nanaze](https://es.discourse.group/u/nanaze)\
**Post date:** [June 16, 2020, 5:18am UTC](https://es.discourse.group/t/map-prototype-addentries-iterable/361/5 "2020-06-16T05:18:14Z")

</div>

@claudiameadows thank you, that looks related.

`merge()` there seems similar, but I'm a bit unclear on the specific semantics (it's not extensively documented). I will read that doc over.

---

<div class="post-metadata">

**Author:** ![nanaze](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nanaze/32/345_2.png) [@nanaze](https://es.discourse.group/u/nanaze)\
**Post date:** [June 16, 2020, 5:39am UTC](https://es.discourse.group/t/map-prototype-addentries-iterable/361/6 "2020-06-16T05:39:01Z")

</div>

found [https://tc39.es/proposal-collection-methods/#Map.prototype.merge](https://tc39.es/proposal-collection-methods/#Map.prototype.merge)

Looks that's like what we want, though `merge()` takes variable arguments.
