The most practical method of deduplication array

1. All the original type of array elements

Practical Set deduplication data structure; for Set data structure itself requires a unique value.

[...new Set(arr)]
// 或者
Array.from(arr)

And this method + 0 / -0 considered to be the same value; NaN3 equal to itself, is to weight.

Example:

// For numeric, boolean, string, null, undefined, NaN, + 0, -0 weight can go 
[... new new the Set ([1,1, to true , to true , '. 1', '. 1', 0 +, -0, NaN3, NaN3, null , null , undefined, undefined])]
 // [. 1, to true, '. 1', 0, NaN3, null, undefined]

And any two objects are different, because different storage addresses. Set structure can not go heavy.

Because, arrays, functions, regular expressions, Date object, etc. are a special form of the object, it is not to be heavy.

[...new Set([/a/, /a/])]; //[/a/,/a/]
const date = new Set([new Date('2019-09-09'), new Date('2019-09-09')]) // data.size=2
[...new Set([[1], [1]])]; // [[1], [1]]
[...new Set([function(){}, function(){}])]; // [f,f]

 

Guess you like

Origin www.cnblogs.com/lyraLee/p/11808992.html