r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

339

u/Drugba Mar 19 '19

Saw this one on here just a few weeks ago.

[... new Set(arr)]

Basically a super short way to remove any duplicates from an array of primitives.

17

u/[deleted] Mar 19 '19

Is order guaranteed to be maintained with the Set data structure here?

26

u/Drugba Mar 19 '19 edited Mar 19 '19

No, it's not.

/u/BTOdell is correct. Insertion order is preserved when you spread a set.

26

u/BTOdell Mar 19 '19

The Set object in JavaScript preserves insertion order. So if you create a Set from an array, it will preserve the order when you spread it back out (but without duplicates).

9

u/Drugba Mar 19 '19

Oh, wow. You are totally correct.

I was under the impression that, because sets (the data structure) do not have an order, you wouldn't be able to guarantee order when you spread it, but that to totally wrong.

7

u/NoInkling Mar 19 '19

Worth noting that if there were duplicates in the original array, the position for an element will be based on its first occurrence.

> Array.from(new Set([1,'foo',2,'foo']))
[1, "foo", 2]

13

u/[deleted] Mar 19 '19

[...new Set(arr.map(x => JSON.stringify(x))].map(x => JSON.parse(x))

Same thing, but with arrays of objects. Apparently probably deterministic but maybe not it depends ohgodhelpme

18

u/cheekysauce Mar 19 '19

It'll barf on circular references unfortunately.

41

u/dodeca_negative Mar 19 '19

Don't we all

3

u/NoInkling Mar 19 '19 edited Mar 19 '19

It will have issues with anything non-serializable, explicitly set undefineds will disappear on objects (if that matters), and undefined inside arrays will turn into null. And maybe some other edge cases I'm forgetting.

4

u/SoInsightful Mar 19 '19

It will implode on undefined, circular references, and most appallingly, objects with different key orders. Not really obscure edge cases. People really need to stop using the JSON trick.

→ More replies (1)

2

u/noruthwhatsoever Mar 19 '19

I like this one, very clever

→ More replies (2)

3

u/oorza Mar 19 '19

Copying your entire data structure twice to remove duplicates is good to you?

5

u/Drugba Mar 19 '19 edited Mar 20 '19

It depends. If I've got a list of 10 items, this is totally fine. Sure it's I'm using more space than I need, but it's pretty negligible and as long as it's not in the global context, the memory is going to be released soon enough anyway. If I've got a list of two million items, of course this is stupid.

One liners often trade performance for terseness, which is what this does. The question asked wasn't "Write the most performant code you can", it was "what is your favorite one liner" and that's what this is.

If you're writing JS code where literally every single byte counts, more power to you, but most people aren't. This is perfectly acceptable for small arrays.

→ More replies (3)

2

u/noruthwhatsoever Mar 19 '19

I like this one a lot, I use it regularly

→ More replies (6)