Javascript 如何从数组中获取唯一值?
我在uniq之前和之后的数据是相同的Javascript 如何从数组中获取唯一值?,javascript,arrays,lodash,Javascript,Arrays,Lodash,我在uniq之前和之后的数据是相同的 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 + " af
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||");
我仍然得到相同的数据函数将为您提供更新阵列的副本 您必须显示此重复阵列:
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||
通过将其转换为集合,然后再转换为数组,可以解决此问题:
var duplicateArray = _.uniq(years);
console.log(duplicateArray + " after uniq");
返回重复的空闲数组。它不修改原始数组。第一步:返回非重复数组的新数组。的可能重复
Array.from(new Set(years));