Search code examples
javascriptarrayslodash

how do I get the unique values from an array?


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||");

My data before uniq and after are the same.

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

I am still getting the same data


Solution

  • The function gives you a duplicate version of your updated array.

    You have to display this duplicate array :

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