admin管理员组文章数量:1410712
Given someArr
, (an array of php
timestamps, which will later be converted to javascript
timestamps (*=1000
)), I was expecting lodash
s...
_.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 ing 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 e 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.
Given someArr
, (an array of php
timestamps, which will later be converted to javascript
timestamps (*=1000
)), I was expecting lodash
s...
_.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 ing 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 e 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.
Share Improve this question edited Mar 25, 2017 at 18:11 tao asked Mar 25, 2017 at 18:03 taotao 90.5k17 gold badges133 silver badges173 bronze badges2 Answers
Reset to default 6_.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
.
According to lodash docs (https://lodash./docs/4.17.4#sortedUniq) _.sortedUniq is for sorted arrays, try using _.uniq()
本文标签: javascriptlodash39s sortedUniq() doesn39t seem to remove duplicates How does it workStack Overflow
版权声明:本文标题:javascript - lodash's .sortedUniq() doesn't seem to remove duplicates. How does it work? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744228873a2596228.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论