i have an array, filled with arrays containing multiple objects. I have a function that returns if an object exists in there or not, but sometimes it cant find an object that is definitely in there. I put a ton of console logs so i can keep track of everything and show you the console. Here is the code:
var adjacent = inChain(x, y - 1, array);
array[adjacent].push({x: x, y: y})
So here i call the inChain function which searches for the object and then returns the index its in. The error is on the 2nd line at push as if it cant find it, adjacent will be null.
Function:
function inChain(x, y, array)
{
var currentPiece = {x: x, y: y};
console.log("checking array below");
console.log(array);
console.log("checking if this object exists");
console.log(currentPiece);
for(let i = blackChains.length; i--;)
{
for(let j = blackChains[i].length; j--;)
{
if (JSON.stringify(blackChains[i][j]) === JSON.stringify(currentPiece))
{
console.log("it does at " + i);
return i;
}
}
}
console.log("doesnt exist");
return null;
}
So as you can see by the console logs, it will say each step, first it will show the array its looking in, then the object its looking for. heres a screenshot of it in action:
So you can see the array contains 1 array which contains 1 object. An object with the properties x: 3, y: 2
Then you can see the object its searching for, an object with the exact same properties
Would anyone know why its sometimes not finding it? and sometimes it does?