2

Given someArr, (an array of php timestamps, which will later be converted to javascript timestamps (*=1000)), I was expecting lodashs...

_.sortedUniq(someArr)

to remove all duplicate values, giving the same result as

Array.from(new Set(someArr))

Could anyone explain why .sortedUniq() doesn't remove duplicates? I also tried _.uniq(), for that matter. Is my assumption wrong? Is there something wrong with my dataset?

Here's the mcve.
The question itself refers to after I define allVals, but I've left the way I'm constructing it in, just in case there's something wrong with how I'm doing that. The initial dataset array is what's coming from php and, for the time being, is not negotiable in terms of structure.

Please note that, while I do have a bit of exercise in javascript, I'm not a "schooled" programmer, I come from a design background and learned to code hands-on, so I'm not excluding the possibility that my grasp of certain programming patterns is not 100% accurate.

Thorough explanations are highly appreciated.

tao
  • 82,996
  • 16
  • 114
  • 150

2 Answers2

5

_.sortedUniq is designed for an array that's already been sorted. Your array has not been sorted.

Replacing it with _.uniq seems to work in removing the duplicates. (JSFiddle)

It is possible to remove duplicates from an array more efficiently if you know it has already been sorted. That, presumably, is why LoDash includes different functions for the two cases - sorted and non-sorted.

By the way uniqKeys === pointKeys will not correctly check whether the two arrays have the same contents, as the arrays are distinct objects even if they contain the same numbers. Instead you need to write a function to do this, or use isEqual.

Community
  • 1
  • 1
Stuart
  • 9,597
  • 1
  • 21
  • 30
  • 1
    I see, so I should actually do `_.sortedUniq(_.uniq(someArr))` - which indeed works. I do believe their current description: *"This method is like _.uniq except that it's designed and optimized for sorted arrays."* is kind of confusing. Upon reading it again, I tend to interpret it as I would first need to sort the array and than use `_.sortedUniq()` on it. While waiting for an answer here I found this which gives the expected output: `[...(new Set(someArr)].sort()`. No idea on performance yet, and it's going to be an issue (i'll typically get ~20k datasets from server). Thank you. – tao Mar 25 '17 at 18:39
  • No, you would need something like `someArr.sort(); var uniques = _.sortedUniq(someArr)`, or just `_.uniq(someArr);` if you don't want to sort it first. – Stuart Mar 25 '17 at 18:43
  • I see. Sort first, remove dupes after. Got it! [`fiddle here`](https://jsfiddle.net/websiter/Lydk7euc/3/). Again, thank you. – tao Mar 25 '17 at 18:51
1

According to lodash docs (https://lodash.com/docs/4.17.4#sortedUniq) _.sortedUniq is for sorted arrays, try using _.uniq()

Anurag
  • 101
  • 1
  • 5