繁体   English   中英

如何在javascript中检查数组是否已满

[英]How to check if an array is full in javascript

我目前正在做一个项目,一旦数组达到 6 个元素,我就必须运行某个函数。 我正在处理一组嵌套数组,我必须检查每个数组是否已达到 6 个元素的容量。 我该怎么做呢? 这是我的 JS

board.every(cell => {
    //checking every array to see if it has 6 elements
    if(cell.length === 7) {
    alert('full!')
      endGame()
    }
}) 

board是父数组, cell代表每个嵌套数组。 cell.length没有解决问题。

查看以下解决方案:

const isFull = board.every(cell => cell.length === 6)

if (isFull) {
   endGame();
   alert('full');
}

您可以使用forEach而不是every

 const cell1 = [1,2,3,4,5,6] const cell2 = [1,2,3,4,5,6] const board = [cell1, cell2] const endgame = () => {console.log('full!')} board.forEach(cell => { //checking every array to see if it has 6 elements if(cell.length === 6) { endgame() } })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM