我想創造的東西,如果多個值是一個數組中,可以檢測,這是我嘗試使用,但總是返回true的Javascript:我如何檢查是否一個數組包含某些價值觀
row7 = [1, 1, 1, 1, 1, 1, 1, 1];
var field = [row0, row1, row2, row3, row4, row5, row6, row7];
console.log(field[0,1]); // Get the first item in the array
var needles = [1, 1, 1, 1]
console.log(needles)
var haystack = field[0,row7]
console.log(haystack)
console.log(contains(haystack,needles));
function contains(haystack, needles) {
return needles.map(function (needle) {
return haystack.indexOf(needle);
}).indexOf(-1) == -1;
}
情況也是一樣的:
row 7 = [1, 1, 1, 1, 1, 1, 1, 1]
(連續4)檢查:
needles = [1, 1, 1, 1]
和假:
row7 = [1, 1, 0, 1, 0, 0, 0, 1]
編輯:
整個數組:
/* Array containing the playing field 8 x 8
C0 C1 C2 C3 C4 C5 C6 C7
R0[0][0][0][0][0][0][0][0]
R1[0][0][0][0][0][0][0][0]
R2[0][0][0][0][0][0][0][0]
R3[0][0][0][0][0][0][0][0]
R4[0][0][0][0][0][0][0][0]
R5[0][0][0][0][0][0][0][0]
R6[0][0][0][0][0][0][0][0]
R7[0][0][0][0][0][0][0][0]
* \
var row0 = [0, 0, 0, 0, 0, 0, 0, 0],
row1 = [0, 0, 0, 0, 0, 0, 0, 0],
row2 = [0, 0, 0, 0, 0, 0, 0, 0],
row3 = [0, 0, 0, 0, 0, 0, 0, 0],
row4 = [0, 0, 0, 0, 0, 0, 0, 0],
row5 = [0, 0, 0, 0, 0, 0, 0, 0],
row6 = [0, 0, 0, 0, 0, 0, 0, 0],
row7 = [0, 0, 0, 0, 0, 0, 0, 0];
var field = [row0, row1, row2, row3, row4, row5, row6, row7];
有沒有辦法實現這個使用純JS?
在此先感謝。
當你做
indexOf
你會得到0是什麼0行... ROW6? – gurvinder372似乎'contains'是指在目標數組中存在的一個值,它返回true。只有當目標數組中不存在所有值時,纔會返回false – kiro
@ gurvinder372添加了整個數組。 – iSidle