两个数组相等测试[重复]

问题描述 投票:1回答:2

这个问题在这里已有答案:

这更像是一个逻辑问题。我无法弄清楚我的功能有什么问题

// This function is meant to check if 3 pixels 
// are colored and therefore if the canvas is full

function checkIfFull() {
    let emptyPixel = [0, 0, 0, 0];
    let pixel1 = get(1, 1);
    //let pixel2 = get(599,599);
    //let pixel3 = get(400, 50);
    console.log(pixel1);

    if (pixel1 === emptyPixel) {
        console.log(true);
    } else {
        console.log(false);
    }
}

我正在运行P5.js库。 get()给我一个数组[a,b,c,d],我试图测试这两者之间的相等或差异(emptyPixel / pixel1)

这些是我尝试过的:测试数组中的每个位置。

谢谢!

javascript arrays equality p5.js
2个回答
0
投票

如果一个空像素是一个数组[0,0,0,0],你可以用这样的东西测试它

function checkIfEmpty(pixel) {
  return pixel.every(x => x === 0)
}

-1
投票

您的数组可以简单地转换为字符串以检查相等性

const emptyPixel = "0,0,0,0";

function checkIfFull() {
    
    const pixel1 = get(1, 1);

    return pixel1.toString() === emptyPixel
}

console.log(checkIfFull)
© www.soinside.com 2019 - 2024. All rights reserved.