# Map.prototype.getOrDefault

**URL:** <https://es.discourse.group/t/map-prototype-getordefault/1384>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [July 3, 2022, 5:16am UTC](https://es.discourse.group/t/map-prototype-getordefault/1384 "2022-07-03T05:16:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![F3n67u](https://avatars.discourse-cdn.com/v4/letter/f/ce7236/32.png) [@F3n67u](https://es.discourse.group/u/F3n67u)\
**Post date:** [July 3, 2022, 5:16am UTC](https://es.discourse.group/t/map-prototype-getordefault/1384/1 "2022-07-03T05:16:41Z")

</div>

Given an array of numbers, I want to build a map in which the key is the number, value is the occurrence time of the number.  
Input: `[1, 2, 2, 3, 3, 3]`  
Output: `Map(3) {1 => 1, 2 => 2, 3 => 3}`

In Java, we could write our program as:

```nohighlight
HashMap<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
   map.put(num, map.getOrDefault(num, 0) + num);
}

```

The key is `getOrDefault` method on `HashMap`, see Java API doc for detail: [Map (Java Platform SE 8 )](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#getOrDefault-java.lang.Object-V-)

In JavaScript, we could write as:

```javascript
const map = new Map();
for (const num of nums) {
  map.set(num, (map.get(num) ?? 0) + 1);
};

```

The javascript version have two problem in my opinion:

1. `map.get(num) ?? 0` is less readable to `map.getOrDefault(num, 0)`
2. `(map.get(num) ?? 0) + 1` is more prone to mistake

On the first try, I write the following program:

```javascript
const map = new Map();
for (const num of nums) {
  map.set(num, map.get(num) ?? 0 + 1);
};

```

If we input `[1, 2, 2, 3, 3, 3]`, the output is `Map(3) {1 => 1, 2 => 1, 3 => 1}`. This is because `map.get(num) ?? 0 + 1` is evaluated as `map.get(num) ?? (0 + 1)` in javascript.

So I am wondering if we could add `Map.prototype.getOrDefault` to javascript, so that we could rewrite the above program to:

```javascript
const map = new Map();
for (const num of nums) {
  map.set(num, map.getOrDefault(num, 0) + 1);
};

```

---

<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:** [July 3, 2022, 12:12pm UTC](https://es.discourse.group/t/map-prototype-getordefault/1384/2 "2022-07-03T12:12:23Z")

</div>

Hi @F3n67u

It sounds like you would be interested in this existing stage 2 proposal:

> **[GitHub - tc39/proposal-upsert: ECMAScript Proposal, specs, and reference...](https://github.com/tc39/proposal-upsert)**
>
> ECMAScript Proposal, specs, and reference implementation for Map.prototype.upsert - GitHub - tc39/proposal-upsert: ECMAScript Proposal, specs, and reference implementation for Map.prototype.upsert

---

<div class="post-metadata">

**Author:** ![alfonsocv12](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/alfonsocv12/32/2085_2.png) [@alfonsocv12](https://es.discourse.group/u/alfonsocv12)\
**Post date:** [May 9, 2024, 12:59am UTC](https://es.discourse.group/t/map-prototype-getordefault/1384/3 "2024-05-09T00:59:21Z")

</div>

How do this work. The proposal has been there for 4 years. is this ever going to get in? what is required for proposals to get into ECMAScript

---

<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:** [May 9, 2024, 2:26am UTC](https://es.discourse.group/t/map-prototype-getordefault/1384/4 "2024-05-09T02:26:31Z")

</div>

> [@alfonsocv12](#):
>
> what is required for proposals to get into ECMAScript

[The TC39 Process](https://tc39.es/process-document/) documents the stage process all proposals must go through. Stage advancement requires full consensus of the committee.

---

<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:** [May 9, 2024, 3:21am UTC](https://es.discourse.group/t/map-prototype-getordefault/1384/5 "2024-05-09T03:21:49Z")

</div>

[https://github.com/tc39/proposal-upsert](https://github.com/tc39/proposal-upsert)
