適用於判斷使用者輸入多個相同型別的資訊,是否重複(如多個身份證號是否重複)
阿新 • • 發佈:2018-12-12
checkRepeat: function (arr) { let arrlist = []; let list = []; let str = ''; for (let i = 0; i < arr.length; i++) { let hasRead = false; for (let k = 0; k < list.length; k++) { if (i === list[k]) { hasRead = true; } } if (hasRead) { continue; } let _index = i, haveSame = false; for (let j = i + 1; j < arr.length; j++) { if (arr[i] === arr[j]) { list.push(j); _index += "," + j; haveSame = true; } } if(_index.length > 1){ arrlist = _index.split(','); for(let m of arrlist){ str += '遊客' + (Number(m)+1) + '、'; } str=str.substring(0,str.length-1); str += '身份證號重複;' } } if(str){ layer.alert(str, { title: '提示' }); console.log(str); return false; }else{ return true; } }
呼叫次函式即可