码迷,mamicode.com
首页 > 编程语言 > 详细

[Tips + Javascript] Make a unique array

时间:2019-01-04 23:33:44      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:console   OLE   move   技术分享   class   ref   java   bee   src   

To make an array uniqued, we can use Set() from Javascript.

const ary = ["a", "b", "c", "a", "d", "c"];
console.log(new Set(ary));

技术分享图片

We can see that all the duplicated value have been removed,  now the only thing we need to do is convert Set to Array.

 

So we have Array.from:

const ary = ["a", "b", "c", "a", "d", "c"];
const res = uniqueArray(ary);
console.log(res);

function uniqueArray(ary) {
  return Array.from(new Set(ary));
}

 

Or even shorter:

const ary = ["a", "b", "c", "a", "d", "c"];
const res = uniqueArray(ary);
console.log(res);

function uniqueArray(ary) {
  return [...new Set(ary)];
}

 Whichever you prefer.

[Tips + Javascript] Make a unique array

标签:console   OLE   move   技术分享   class   ref   java   bee   src   

原文地址:https://www.cnblogs.com/Answer1215/p/10222975.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!