【问题标题】:how do I get the unique values from an array?如何从数组中获取唯一值?
【发布时间】:2018-04-03 13:48:04
【问题描述】:
var years = this.api().columns(2).data().toArray().sort();
console.log(years + " before uniq");
var duplicateFreeYears = Array.from(new Set(years));
const uniqueTopicList = _.uniq(years)
console.log("||" + duplicateFreeYears + " after duplicateFreeYears||");
console.log("||" + uniqueTopicList + " after uniqueTopicList||");

我在uniq之前和之后的数据是一样的。

2016,2016,2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2015 before uniq


||2016,2016,2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2015 after duplicateFreeYears||


||2016,2016,2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2015 after uniqueTopicList||

我仍然得到相同的数据

【问题讨论】:

标签: javascript arrays lodash


【解决方案1】:

该函数为您提供更新后数组的重复版本。

你必须显示这个重复的数组:

var duplicateArray = _.uniq(years);
console.log(duplicateArray + " after uniq");

【讨论】:

    【解决方案2】:

    有一种解决方法,将其转换为集合,然后再次转换为数组:

    Array.from(new Set(years));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-20
      • 2011-03-02
      • 1970-01-01
      • 2020-09-22
      • 2019-01-03
      • 1970-01-01
      相关资源
      最近更新 更多