lodash.uniq不起作用 [英] lodash.uniq do not work

查看:229
本文介绍了lodash.uniq不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

uniq 返回一个新数组,而不是对现有数组进行变异.

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

从文档中

使用SameValueZero进行相等性比较,创建一个数组的无重复版本,其中仅保留每个元素的第一个匹配项.结果值的顺序取决于它们在数组中出现的顺序.

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)

这篇关于lodash.uniq不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆