如何知道具有数字(零)的数组是否被视为空数组?

时间:2016-05-13 16:50:47

标签: javascript arrays

我使用一组数字,每个数字代表数字零的颜色。就像你在我的代码中看到的那样,我绘制了一个矩形数组,数字为零,我清除了矩形,这些矩形变成了一个空位。我怎么知道我的数组是否为空?如果我的数组中仍然存在零,那么这是一个空数组呢?



	var canvas = document.getElementById("mijnCanvas");
	var mijnObject = canvas.getContext("2d");

	function tekenenObjecten() {
		makenMuur();
	}
	setInterval(tekenenObjecten, 20);

	var stenenPerRij = 27;
	var steenHoogte = 20;
	var steenBreedte = canvas.width/stenenPerRij;

	var stenen = [
		[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
		[0,1,1,1,1,1,1,1,1,1,1,1,6,1,1,1,1,1,1,1,1,1,1,1,1,1,0],
		[0,1,2,2,2,2,2,2,7,2,2,2,2,2,2,2,2,2,2,7,2,2,2,2,2,1,0],
		[0,1,1,6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,6,1,1,1,1,0],
		[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
		[0,1,3,0,0,2,2,2,2,0,0,0,0,0,0,1,0,2,2,2,2,0,1,0,0,0,1],
		[1,0,0,1,0,2,0,0,0,2,0,0,0,0,0,1,0,2,0,0,0,0,1,3,0,0,1],
		[1,1,1,1,0,2,2,2,2,0,0,0,0,0,0,1,0,4,2,2,0,0,1,0,1,0,1],
		[1,0,0,1,0,2,0,2,0,0,0,1,0,0,0,1,0,2,0,0,0,0,1,0,0,1,1],
		[1,0,0,1,0,2,0,0,2,2,0,0,1,1,1,0,0,2,2,2,2,0,1,0,0,0,1],
		[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
		[0,1,1,1,1,1,1,1,6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0],
		[0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,7,2,2,2,2,2,2,2,2,1,0],
		[0,1,1,1,1,1,1,1,1,6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0],
		[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	];

	function makenMuur() {
		for(var i = 0; i < stenen.length; i = i+1) {
			for(var j = 0; j < stenen[i].length; j = j+1) {
				tekenenStenen(j,i,stenen[i][j]);
			}
		}
	}

	function tekenenStenen(x,y,stenen) {
		switch(stenen) {
			case 1:
				mijnObject.fillStyle = "#0D0D0D";
				break;
			case 2:
				mijnObject.fillStyle = "#CCCCCC";
				break;
			case 3:
				mijnObject.fillStyle = "#0D0D0D";
				break;
			case 4:
				mijnObject.fillStyle = "#CCCCCC";
				break;
			default:
				mijnObject.clearRect(0, 0, steenBreedte, steenHoogte);
				break;
		}
		if(stenen) {
			mijnObject.beginPath();
			mijnObject.strokeStyle = "#000000";
			mijnObject.rect(x*steenBreedte, y*steenHoogte, steenBreedte, steenHoogte);
			mijnObject.fill();
			mijnObject.stroke();
			mijnObject.closePath();
		}
	}
&#13;
<body>
    <canvas id="mijnCanvas" width="1200" height="900"></canvas>
    <script src="../BrickSmasher.js"></script>
</body>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:3)

使用Array.every,您可以检查是否在所有行中,所有列都是0.这个解决方案的好处是,如果第一行在第一列上没有0,那么它将返回false而不检查数组的其余部分

var isEmpty = stenen.every(function(row){ // does every row have all 0 in its columns?
    return row.every(function(col){ // do all columns in a row have all 0?
        return col == 0
    })
})

正如@DonovanM指出的那样,使用ES6你可以做到:

let isEmpty = stenen.every(row => row.every(col => col === 0))

答案 1 :(得分:1)

使用reduce迭代您的数组,汇总所有数字,然后将其返回。 (请参阅文档,了解具体方法)。如果sum为0,则表示数组为空。

let sum = stenen.reduce((x,y) => x+y); //sums up (using an arrow function) the array 
if (!stenen) // the sum is 0 and it's "empty"

注意:这是ES6代码。这将(从单行暗示返回)转换为:

var sum = stenen.reduce(function(x,y) {
    return x+y;
});

答案 2 :(得分:0)

您可以编写一个函数来检查数组中任何位置的非零值。

function isEmpty(arr) {
  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr[i].length; j++) {
      if (arr[i][j] !== 0) {
        return false;
      }
    }
  }
  return true;
}

或者是一个更快的版本,用于缓存数组的长度。

function isEmpty(arr) {
  for (var i = 0, len = arr.length; i < len; i++) {
    var subArr = arr[i];
    for (var j = 0, len2 = subArr.length; j < len2; j++) {
      if (subArr[j] !== 0) {
        return false;
      }
    }
  }
  return true;
}

答案 3 :(得分:0)

你可以在遍历每个数组时使用过滤器来检查长度大于0:

function check(matrix) {
  for (var i = 0; i < matrix.length; i++) {
    var len = matrix[i].filter(function(el) {
      return el > 0;
    }).length;
    if (len) {
      return false;
    }
  }
  return true;
}

Working Example