配列の中の値がユニークか調べる

 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


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS

Last-modified: 2020-06-10 (水) 18:33:19