javascript 多维布尔数组检查Javascript中是否全部为真
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10097136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multidimensional boolean array checking if all true in Javascript
提问by Kirberry
I have a multidimensional array of bools, with each element set to true:
我有一个多维布尔数组,每个元素都设置为 true:
var boolarray= $.extend(true, [], board);
var boolarray= $.extend(true, [], board);
board is a 3x3 multidimensional array of strings. boolarray is simply a deep copy of this.
board 是一个 3x3 的多维字符串数组。boolarray 只是这个的深层副本。
for (var i=0; i < boolarray.length; i++) {
boolarray[i]
for (var j=0; j < boolarray[i].length; j++) {
boolarray[i][j] = true;
};
};
This gives me:
这给了我:
boolarray = [true,true,true,true,true,true,true,true,true]
boolarray = [真、真、真、真、真、真、真、真、真]
I want to check if all elements are true and return if this is the case. However my method below does not work.
我想检查所有元素是否为真,如果是这种情况则返回。但是我下面的方法不起作用。
if (boolarray == true)
{
console.log("all elements in boolarray are true, return true ")
return true;
}
else
{
console.log("not all elements in boolarray are true, return false")
return false;
}
Any ideas of how I should check if boolarray is all true?
关于我应该如何检查 boolarray 是否全部正确的任何想法?
采纳答案by Luke Merrett
You would need to loop through the array again to check all the values; in a very similar way to how you assigned them:
您需要再次遍历数组以检查所有值;以与您分配它们的方式非常相似的方式:
for (var i=0; i < boolarray.length; i++) {
for (var j=0; j < boolarray[i].length; j++) {
if (boolarray[i][j] == false) {
return false;
}
};
};
return true;
回答by
Use .every()
...
使用.every()
...
var boolarray = [true,true,true,true,true,true,true,true,true];
boolarray.every(Boolean);
DEMO:http://jsfiddle.net/gFX7X/
演示:http ://jsfiddle.net/gFX7X/
If the only purpose of the first loop was to create the second, then you could skip it and do this...
如果第一个循环的唯一目的是创建第二个循环,那么您可以跳过它并执行此操作...
var boolarray = [[true, true, true],
[true, true, true],
[true, true, true]];
boolarray.every(function(arr) {
return arr.every(Boolean)
});
DEMO:http://jsfiddle.net/gFX7X/1/
演示:http ://jsfiddle.net/gFX7X/1/
Or a slightly shorter version of the previous one:
或者是上一个略短的版本:
boolarray.every(Function.call.bind(Boolean, null))
回答by Paul Sullivan
As an alternative to using a boolean array why not use a simple Hexidecimal number to store your board (and then use bit manipulation to change/test) i.e.
作为使用布尔数组的替代方法,为什么不使用简单的十六进制数来存储您的电路板(然后使用位操作来更改/测试)即
000 000 001
000 000 001
==
==
1decimal
1个十进制
111 111 111
111 111 111
==
==
511 (256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1)
511 (256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1)
Setting a board position true or false would then become a bit manipulation and testing would become as simple as parseInt = 511...
将棋盘位置设置为 true 或 false 将变得有点操作,测试将变得像 parseInt = 511 一样简单...
回答by The Alpha
回答by Novak
Write a function that runs a loop on a given parameter (our 2d array), and checks each cell if true. If not, return false. After the loop, return true;
编写一个对给定参数(我们的二维数组)运行循环的函数,并检查每个单元格是否为真。如果不是,则返回false。循环后,返回true;
回答by Dion
bool alltrue = true;
for (var i=0; i < boolarray.length; i++) {
boolarray[i]
for (var j=0; j < boolarray[i].length; j++) {
if(boolarray[i][j] != true) {
alltrue = false;
}
};
};
回答by Madbreaks
for(var i in boolarray)
if(!boolarray[i]) return false;
return true;
...this is based on your statement:
...这是基于您的陈述:
boolarray = [true,true,true,true,true,true,true,true,true]
boolarray = [真、真、真、真、真、真、真、真、真]
Which is NOT a multi-dimensional array.
这不是多维数组。
回答by jAndy
ES5 notation example (simplified):
ES5 符号示例(简化):
var foo = [ [true, true, true], [true, true], [true, true], [false, true], [true, true] ];
var boolarray = foo.every(function(level2, i) {
return level2.every(function(bool) {
return bool;
});
});
This example exploits the fact that Array.prototype.every
returns the result which returned from the loop function. As soon as a falsy
value is returned, the iteration stops aswell.
此示例利用Array.prototype.every
返回从循环函数返回的结果的事实。一旦falsy
返回值,迭代也会停止。
If you need to stay compatible with old'ish browsers live IE6/7 you can just download one of the many ES5 shim librarys out there
如果您需要与旧版浏览器实时 IE6/7 保持兼容,您只需下载众多 ES5 填充库之一
回答by Ricardo Garza V.
your variable named boolArray is an array and as long as it is not null, the code you wrote will se it as true, to get what you want you need something like this:
您名为 boolArray 的变量是一个数组,只要它不为空,您编写的代码就会将其设置为 true,要获得您想要的东西,您需要这样的东西:
var boolArrayValue = true; // valor final a usar es boolarray value
for(var i = 0;i<boolarray.length;i++){
for(var a = 0;a<boolarray[i].length;a++){
boolArrayValue = boolArrayValue && boolarray[i][a];
}
}
回答by Wayne
A recursive version that checks nested arrays of arbitrary depth:
检查任意深度的嵌套数组的递归版本:
function deepEvery(arr, fn, _this) {
return (function iterate(arr, i) {
var len = arr && arr.length;
if (!len || i >= len) {
return false;
}
var first = arr[i] && typeof arr[i].splice === "function"
? iterate(arr[i], 0)
: fn.call(_this || this, arr[i], i);
i += 1;
return !!first && (i >= len || iterate(arr, i));
})(arr, 0);
}
Usage:
用法:
deepEvery([[true, true, true],
[true, true, true],
[true, true, true]], function(el, i) {
return el;
});
Note that this allows for any type of check in the callback. If the function you pass to deepEvery
returns false
for any element, then the overall result is false
; otherwise, the result is true
. Example:
请注意,这允许在回调中进行任何类型的检查。如果您传递给的函数为任何元素deepEvery
返回false
,则总体结果为false
;否则,结果为true
。例子:
deepEvery([4, true, [6, [5, "F"], 6]], function(el, i) {
return typeof el === "number";
});