我想创造的东西,如果多个值是一个数组中,可以检测,这是我尝试使用,但总是返回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