# Array.prototype.difference

**URL:** <https://es.discourse.group/t/array-prototype-difference/1807>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [September 25, 2023, 8:05pm UTC](https://es.discourse.group/t/array-prototype-difference/1807 "2023-09-25T20:05:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![behnammodi](https://avatars.discourse-cdn.com/v4/letter/b/7ba0ec/32.png) [@behnammodi](https://es.discourse.group/u/behnammodi)\
**Post date:** [September 25, 2023, 8:05pm UTC](https://es.discourse.group/t/array-prototype-difference/1807/1 "2023-09-25T20:05:05Z")

</div>

JavaScript's built-in `Array` object does not have a `difference` method by default to compare 2 arrays. However, it looks very useful to have it as a bulit-in method:

```javascript
const a1 = [1, 2, 3]
const a2 = [1, 3]
a1.difference(a2);
// result: [2]

```

It can accept a custom comparator function. This will give us more flexibility in comparing elements. Here's an example:

```javascript
const a1 = [1.2, 2.3, 3.9]
const a2 = [1.4, 3.8]
a1.difference(a2, (a, b) => Math.round(a) === Math.round(b));
// result: [2.3]

```

If we don't provide a comparator function, a default one is assumed to work like this:

```javascript
(a, b) => a === b

```

---

<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:** [September 25, 2023, 9:51pm UTC](https://es.discourse.group/t/array-prototype-difference/1807/2 "2023-09-25T21:51:16Z")

</div>

Related: this is being added to Sets:

> **[GitHub - tc39/proposal-set-methods: Proposal for new Set methods in JS](https://github.com/tc39/proposal-set-methods)**
>
> Proposal for new Set methods in JS. Contribute to tc39/proposal-set-methods development by creating an account on GitHub.

```javascript
const s1 = new Set([1, 2 ,3]);
const s2 = new Set([1, 3]);
s1.difference(s2);
// result: new Set([2])

```

---

<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:** [September 25, 2023, 10:40pm UTC](https://es.discourse.group/t/array-prototype-difference/1807/3 "2023-09-25T22:40:32Z")

</div>

Please do not recommend that people modify objects they don't own; it's not a polyfill until it's in a spec.

I think the main issue here is that your suggestion uses `.includes` which uses `SameValue` - it makes a lot more sense to me to take a comparator function that decides whether two things are the same or different.

---

<div class="post-metadata">

**Author:** ![behnammodi](https://avatars.discourse-cdn.com/v4/letter/b/7ba0ec/32.png) [@behnammodi](https://es.discourse.group/u/behnammodi)\
**Post date:** [September 26, 2023, 3:58am UTC](https://es.discourse.group/t/array-prototype-difference/1807/4 "2023-09-26T03:58:17Z")

</div>

@ljharb Thank you for your input. I've removed the polyfill and incorporated your suggestion to use a comparator function. I appreciate the idea.

---

<div class="post-metadata">

**Author:** ![khaosdoctor](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/khaosdoctor/32/1904_2.png) [@khaosdoctor](https://es.discourse.group/u/khaosdoctor)\
**Post date:** [November 8, 2023, 10:17am UTC](https://es.discourse.group/t/array-prototype-difference/1807/5 "2023-11-08T10:17:02Z")

</div>

This can also be extended to `Array.intersection`, which is basically the same idea but opposite

---

<div class="post-metadata">

**Author:** ![behnammodi](https://avatars.discourse-cdn.com/v4/letter/b/7ba0ec/32.png) [@behnammodi](https://es.discourse.group/u/behnammodi)\
**Post date:** [November 8, 2023, 11:37am UTC](https://es.discourse.group/t/array-prototype-difference/1807/6 "2023-11-08T11:37:34Z")

</div>

I like your idea
