You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

array-unique-proposal

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-unique-proposal

ECMAScript proposal for Deduplicating method of Array

0.3.4
latest
Source
npmnpm
Version published
Weekly downloads
16
-75.76%
Maintainers
1
Weekly downloads
 
Created
Source

Array deduplication proposal

ECMAScript proposal for Deduplicating method of Array.

Proposal Stage-1 CI & CD

NPM

Motivation

Deduplication is one of the most common requirements in Data processing, especially in large Web Apps nowadays.

In Lodash, *uniq* methods are also very popular:

#NameDownloads
1uniq5,546,070
7uniqby447,858
28uniqwith15,077

But [...new Set(array)] in ECMAScript 6 isn't enough for Non-primitive values, and now, we may need a Array.prototype.uniqueBy().

Core features

While Array.prototype.uniqueBy() invoked with:

  • no parameter, it'll work as [...new Set(array)];

  • one index-key parameter (Number, String or Symbol), it'll get values from each array element with the key, and then deduplicates the origin array based on these values;

  • one function parameter, it'll call this function for each array element, and then deduplicates the origin array based on these returned values.

Notice:

  • the Returned value is a new array, no mutation happens in the original array
  • Empty/nullish items are treated as nullish values
  • 0 & -0 are treated as the same
  • All NaNs are treated as the same

Typical cases

[1, 2, 3, 3, 2, 1].uniqueBy();  // [1, 2, 3]

const data = [
    { id: 1, uid: 10000 },
    { id: 2, uid: 10000 },
    { id: 3, uid: 10001 }
];

data.uniqueBy('uid');
// [
//     { id: 1, uid: 10000 },
//     { id: 3, uid: 10001 }
// ]

data.uniqueBy(({ id, uid }) => `${id}-${uid}`);
// [
//     { id: 1, uid: 10000 },
//     { id: 2, uid: 10000 },
//     { id: 3, uid: 10001 }
// ]

Polyfill

A polyfill is available in the core-js library. You can find it in the ECMAScript proposals section.

A simple polyfill from the proposal repo write in TypeScript.

Proposer

Keywords

JavaScript

FAQs

Package last updated on 16 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts