1
u/__ibowankenobi__ Nov 13 '19
now go do that with objects.
1
u/Chawki_ Nov 13 '19
what you mean, returning unique objects?
1
u/__ibowankenobi__ Nov 13 '19
aha, that wouldn't work would it.
2
u/Ebola300 Nov 13 '19
You could assign to object keys and then return the result of Objext.keys()
3
1
u/dhudhudhadha Nov 13 '19
What?
1
u/Ebola300 Nov 13 '19
Typo. Another way to find uniques in an array is to add them to an object as a property the get all the keys using Object.keys(). Since an object cannot have duplicate keys, you always get unique values.
7
u/all_things_code Nov 13 '19
Shallow copy.
arr2 = [5,4,[6,5],4,3,[6,5]];
[... new Set(arr2)]
//the inner arrays make it out4
Nov 13 '19
Those inner arrays are not the same though. Two arrays or two objects, even if they have the same contents, are distinct values from one another. It would be extremely bad if this didn't happen.
1
u/all_things_code Nov 13 '19
Close. They are references (think memory addresses). They have different memory addresses. Therefore, not the same or equal. I believe the great JS ninja /u/senocular stated it thus:
"Comparing two objects (no matter if with == or ===) will always use identity comparison, not value comparison. They would only be equal if they were references to the same object."
1
Nov 13 '19
Thanks, I appreciate it, but I know that, and my comment is not 'close', it's just slightly less precise given that your original comment seemed to indicate a fairly low level of understanding of value and reference.
0
1
u/YojG Nov 13 '19
What
2
u/arkdelkaos Nov 13 '19
https://imgur.com/a/inUqFIR
As you can see both [6,5] sub inner arrays are listed after the Set.1
u/eggtart_prince Nov 13 '19
Deep object/array copying is too expensive. I don't think there is any native function or class that deep copies.
0
u/all_things_code Nov 13 '19
On my phone, but theres a neat way where you stringify the array, then later de-stringify it. Deep copy achieved, low overhead.
2
14
u/robotsympathizer Nov 13 '19
I saw this as a solution to a Leetcode problem where the challenge was to remove duplicates from a sorted array of integers.