如何检查二维数组中的每一项是否符合特定条件?
How to check every item in a 2D array for specific condition?
:) 我正在使用 JS 和 P5 创建一个迷宫,其中有一个二维数组,其中填充了数字 0-8。 0 是空位,1 是墙,2 是你走路的角色,3 是出口,4-8 是随机产生的物品。为了退出迷宫(通过3,设置在一个固定的点),需要收集所有物品(如果你走过一个物品,这个点的值变回0),所以数组中的每个值应该低于 4 才能退出。现在我需要一种方法来检查是否是这种情况。
我用 every() 试过了,但我想这只适用于常规数组。我想我需要一个 for 循环,但我不知道这应该是什么样子。这就是我需要帮助的地方!
我的迷宫由 18 行和 18 列组成,像这样(但还有 15 行)
let maze = [
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,2,0,0,0,0,1,0,1,0,0,0,0,1,0,0,1,0,1,0,0,0,3],
[1,1,1,1,1,0,1,0,0,0,1,1,0,0,0,0,1,0,1,0,1,0,1]
]
物品随机生成,这已经有效。现在我尝试检查每个值是否 <= 3,每个值都是这样
function checkBoard(mazenumbers){
return mazenumbers <= 3;
}
function alertMazenumbers() {
alert(maze.every(checkBoard));
}
并希望在您走进出口位置时通过警报显示,就像这样
else if(direction === 'right') {
if(maze[playerPos.y][playerPos.x + 1] == 3) {
alertMazenumbers();
}
如果每个值 <= 3,我想得到一个 true 的警报,否则得到一个 false 的警报。
目前,使用这个 every(),我确实收到了警报,但它只有 return 是假的,即使所有项目都被清除并且它应该 return 是真的。
方法 1:检查是否每个数组只包含 <= 3
的数字
let maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 3],
[1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1]
];
function testMaze(maze) {
return maze.every(row => row.every(itemIsValid));
}
function itemIsValid(item) {
return item <= 3;
}
console.log(testMaze(maze));
maze[2][4] = 4;
console.log(testMaze(maze));
方法2:合并数组并搜索数字
var maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 3],
[1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1]
];
function testMaze(maze) {
return [].concat(...maze).every(itemIsValid);
}
function itemIsValid(item) {
return item <= 3;
}
console.log(testMaze(maze));
maze[2][4] = 4;
console.log(testMaze(maze));
方法三:将迷宫转为字符串并使用正则表达式
var maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 3],
[1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1]
];
function testMaze(maze) {
//or maze.toString().match(/\d+/g).every(x => itemIsValid(+x));
return !/[4-8]/g.test(`${maze}`);
}
function itemIsValid(item) {
return item <= 3;
}
console.log(testMaze(maze));
maze[2][4] = 4;
console.log(testMaze(maze));
你可以通过这样做
检查迷宫中的每个点是否都是<=3
const isTrue = num => num <= 3; // is a single cell true
const isRowTrue = row => row.every(isTrue); // are all cells in a row true
const isMazeTrue = rows => rows.every(isTrue); // are all cells in all rows true
const maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 3],
[1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1]
];
console.log(isMazeTrue(maze));
使用 every
!
您走在正确的轨道上
迷宫是一个数组的数组(正如 Denys 在他的评论中提到的),所以你必须使用 every
两次,如下所示:
function canExitMaze(maze) {
return maze.every(row => row.every(cell => cell <= 3))
}
如果您不认识箭头函数语法 (=>
),this article 会对其进行解释。
希望对您有所帮助!
:) 我正在使用 JS 和 P5 创建一个迷宫,其中有一个二维数组,其中填充了数字 0-8。 0 是空位,1 是墙,2 是你走路的角色,3 是出口,4-8 是随机产生的物品。为了退出迷宫(通过3,设置在一个固定的点),需要收集所有物品(如果你走过一个物品,这个点的值变回0),所以数组中的每个值应该低于 4 才能退出。现在我需要一种方法来检查是否是这种情况。
我用 every() 试过了,但我想这只适用于常规数组。我想我需要一个 for 循环,但我不知道这应该是什么样子。这就是我需要帮助的地方!
我的迷宫由 18 行和 18 列组成,像这样(但还有 15 行)
let maze = [
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,2,0,0,0,0,1,0,1,0,0,0,0,1,0,0,1,0,1,0,0,0,3],
[1,1,1,1,1,0,1,0,0,0,1,1,0,0,0,0,1,0,1,0,1,0,1]
]
物品随机生成,这已经有效。现在我尝试检查每个值是否 <= 3,每个值都是这样
function checkBoard(mazenumbers){
return mazenumbers <= 3;
}
function alertMazenumbers() {
alert(maze.every(checkBoard));
}
并希望在您走进出口位置时通过警报显示,就像这样
else if(direction === 'right') {
if(maze[playerPos.y][playerPos.x + 1] == 3) {
alertMazenumbers();
}
如果每个值 <= 3,我想得到一个 true 的警报,否则得到一个 false 的警报。 目前,使用这个 every(),我确实收到了警报,但它只有 return 是假的,即使所有项目都被清除并且它应该 return 是真的。
方法 1:检查是否每个数组只包含 <= 3
的数字
let maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 3],
[1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1]
];
function testMaze(maze) {
return maze.every(row => row.every(itemIsValid));
}
function itemIsValid(item) {
return item <= 3;
}
console.log(testMaze(maze));
maze[2][4] = 4;
console.log(testMaze(maze));
方法2:合并数组并搜索数字
var maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 3],
[1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1]
];
function testMaze(maze) {
return [].concat(...maze).every(itemIsValid);
}
function itemIsValid(item) {
return item <= 3;
}
console.log(testMaze(maze));
maze[2][4] = 4;
console.log(testMaze(maze));
方法三:将迷宫转为字符串并使用正则表达式
var maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 3],
[1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1]
];
function testMaze(maze) {
//or maze.toString().match(/\d+/g).every(x => itemIsValid(+x));
return !/[4-8]/g.test(`${maze}`);
}
function itemIsValid(item) {
return item <= 3;
}
console.log(testMaze(maze));
maze[2][4] = 4;
console.log(testMaze(maze));
你可以通过这样做
检查迷宫中的每个点是否都是<=3
const isTrue = num => num <= 3; // is a single cell true
const isRowTrue = row => row.every(isTrue); // are all cells in a row true
const isMazeTrue = rows => rows.every(isTrue); // are all cells in all rows true
const maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 3],
[1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1]
];
console.log(isMazeTrue(maze));
使用 every
!
迷宫是一个数组的数组(正如 Denys 在他的评论中提到的),所以你必须使用 every
两次,如下所示:
function canExitMaze(maze) {
return maze.every(row => row.every(cell => cell <= 3))
}
如果您不认识箭头函数语法 (=>
),this article 会对其进行解释。
希望对您有所帮助!