我正在測試針對某些字符的數組,我想知道爲什麼它通過數組循環4次。我只是想通過toTest
循環一次。下面是代碼和小提琴爲什麼這是循環遍歷數組的4次?
var testAgainst = ['and','+',',','&'];
var toTest = ["$ Red Arcane Playing Cards Deck Ellusionist Rare New Sealed $", "Ellusionist Red Arcane Deck - Very Rare - Playing Cards - Magic Tricks- New", "24 HR Sale! Red and White LTD & Red Arcane Playing Cards Ellusionist near mint", "4x Arcane Playing Cards Deck Collection Rare Gold Red + Black White Ellusionist", "Red Arcane Playing Cards by Ellusionist", "24 HR SALE Red Arcane Playing Cards by Ellusionist", "RED ARCANE PLAYING CARDS", "Ellusionist - New/Sealed Red Arcane Playing cards c.2013 - Rare deck"];
for(var i = 0; i < testAgainst.length; i++){
for(var j = 0; j < toTest.length; j++){
if (toTest[j].indexOf(testAgainst[i]) > -1){
console.log('match: ' + toTest[j]);
}else{
console.log('no match ' + toTest[j]);
}
}
}
小提琴:https://jsfiddle.net/bmpgsxb2/4/
爲什麼會循環經歷了這麼多?
感謝所有的人指出我正確的方向。我應該意識到這是怎麼回事。玩了一段時間後我來到了這一點,這只是對數據進行過濾一次,並且會將數據高達取決於它是否符合標準或不
的jsfiddle UPDATE:https://jsfiddle.net/bmpgsxb2/7/
難道你忘了代碼中的break語句嗎? – Serjik
由於testAgainst中有4個項目? –
因爲你有兩個嵌套循環,並且你通過'toTest'循環多次,因爲你有'testAgain'中的項目...! – deceze