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

  1. const ary = ["a", "b", "c", "a", "d", "c"];
  2. 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:

  1. const ary = ["a", "b", "c", "a", "d", "c"];
  2. const res = uniqueArray(ary);
  3. console.log(res);
  4.  
  5. function uniqueArray(ary) {
  6. return Array.from(new Set(ary));
  7. }

Or even shorter:

  1. const ary = ["a", "b", "c", "a", "d", "c"];
  2. const res = uniqueArray(ary);
  3. console.log(res);
  4.  
  5. function uniqueArray(ary) {
  6. return [...new Set(ary)];
  7. }

Whichever you prefer.

[Tips + Javascript] Make a unique array的更多相关文章

  1. tips javascript(一)

    tips javascript(一) 实现type函数用于识别标准类型和内置对象类型,语法如下: var t = type(obj); function type(o){    if (o === n ...

  2. Javascript语言精粹之Array常用方法分析

    Javascript语言精粹之Array常用方法分析 1.Array常用方法分析 1.1 Array.prototype.sort() Javascript的默认比较函数假定被排序元素都是字符串,所以 ...

  3. 思维导图(自己整理,希望对大家有用):JavaScript函数+canvas绘图+Array数组

    1.javascript函数: 2.Array数组: 3.canvas绘图:

  4. javascript 数组去重 unique

    晚上无事,偶然看到这么个小测试,拿来写一写,希望大家提建议: 直接上代码: Array.prototype.unique = function (isStrict) { if (this.length ...

  5. JavaScript学习笔记之Array

    数组的定义: 1,var arr=new Array();      -->数组是特殊的对象,typeOf的返回值是object arr[0] arr[1] ... 2,var arr=new ...

  6. JavaScript的json和Array及Array数组的使用方法

    1.关于json JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集.也可以称为数据集和数组类似,能够存数据! //Ar ...

  7. JavaScript,通过分析Array.prototype.push重新认识Array

    在阅读ECMAScript的文档的时候,有注意到它说,数组的push方法其实不仅限于在数组中使用,专门留作通用方法.难道是说,在一些类数组的地方也可以使用?而哪些是和数组非常相像的呢,大家或许一下子就 ...

  8. Javascript学习总结三(Array对象的用法)

    javascript Array对象的常用API 1:concat concat() 方法用于连接两个或多个数组.该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本.举例:var a1 = [ ...

  9. Javascript中String、Array常用方法介绍

    string和array作为javascript内置对象,其中许多方法无论是在开发过程中,还是在面试的时候都有机会被面试官问到,这里对经常用到的方法做一个介绍,这些方法都有过很多的实际应用场景,所以对 ...

随机推荐

  1. parallel programming. this causual litery nots represents my recent progress in parallel programming in c#.It`s interesting.

    not to say extra words,let`s start the code. pasted below: using System; using System.Collections.Ge ...

  2. 从jscript脚本混淆说起

    转载:http://www.freebuf.com/column/144897.html 脚本病毒是一个一直以来就存在,且长期活跃着的一种与PE病毒完全不同的一类病毒类型,其制作的门槛低.混淆加密方式 ...

  3. mysql故障(程序正在运行却找不到PID文件)

    [root@slave ~]# /etc/init.d/mysql status ERROR! MySQL is running but PID file could not be found [ro ...

  4. .apache.commons.io 源代码学习(一)

    java的初学者,准备通读各种高水平源代码,提升能力. 为了避免自己的惰性,写博客. 版本:2.5 开发平台:netbeans. 今天是第一天,网上先看个例子:http://www.importnew ...

  5. hdu 5138(水题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5138 反着来. #include<iostream> #include<cstdi ...

  6. hdu 1301(最小生成树)

    Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  7. 可视化web日志分析工具Logstalgia

    https://blog.csdn.net/zrools/article/details/47250661

  8. Reporting Services的简单使用

    最近公司的功能需要使用报表,用的是微软自带的报表,谈一谈我们的做法,希望可以给想学习的人一些指导 1:新建報表所需的數據源DataSet.cs using System; using System.C ...

  9. HDU 5880 Family View (2016 青岛网络赛 C题,AC自动机)

    题目链接  2016 青岛网络赛  Problem C 题意  给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fa ...

  10. strace 使用案例

    http://www.cnblogs.com/lixigang/articles/5512527.html