在 JavaScript 中检查现有数组是否包含所有不同元素的最佳方法?

Best way to check if an existing array is having all distinct elements or not in JavaScript?

像这样:

let a = [2, 34, 'dafsd', null, {}];
let b = [null,  null] -or- ['same','same'] -or- [100, 100]
isDistinct(a) // => true
isDistinct(b) // => false

您可以取 Set 个项目并根据集合的大小检查数组的长度。如果相等,则所有元素都是唯一的。

let a = [2, 34, 'dafsd', null, {}];

console.log(a.length === new Set(a).size);