JS array deduplication

One, ES6 array deduplication [...new Set(array)]

var arr = [1,1,3,5,3,4,4,8,7,8];


//method one:
    function unique (arr) { 
         const seen = new Map() 
         return arr.filter( (a) => !seen.has(a) && seen.set(a, 1))
    }
// or method two
function unique (arr) { 
     return Array.from(new Set(arr))  // The Array.from method can convert the Set structure to an array.
}
// or method three
var arr2 = [...new Set(arr)];


Author: You Yuxi
Link: https://www.zhihu.com/question/29558082/answer/44854426
Source: Zhihu
The copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324821872&siteId=291194637