配列の中の値がユニークか調べる const array1 = [1,2,3,1];
if (([...new Set(array1)]).length == array1.length) {
console.log("unique");
} else {
console.log("not unique");
}
=> not unique
const array2 = [1,2,3];
if (([...new Set(array2)]).length == array2.length) {
console.log("unique");
} else {
console.log("not unique");
}
=> unique
参考https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array/9229821#9229821 |
|