lodash.uniq do not work

北城以北 提交于 2019-12-11 08:53:16

问题


<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.min.js"></script>

<script>
    var topicData = []; //some data here

    var topicList = topicData.map((d) => {
        return d.topic;
    })
    _.uniq(topicList); //not work, still the old array

</script>

I wonder whether I import the library wrong or something else. Any help is appreciated.


回答1:


uniq returns a new array instead of mutating the existing one.

From the docs:

Creates a duplicate-free version of an array, using SameValueZero for equality comparisons, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the array.

So you should do something like the following.

const uniqueTopicList = _.uniq(topicList)


来源:https://stackoverflow.com/questions/45662804/lodash-uniq-do-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!