pyy
pyy

Reputation: 965

lodash.uniq do not work

<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.

Upvotes: 1

Views: 3758

Answers (1)

Lazar Ljubenović
Lazar Ljubenović

Reputation: 19764

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)

Upvotes: 3

Related Questions