I have an small array of data which i want to check if my values im checking against exist in the array together.
This is how the data looks like from my console.log(astar);
https://i.stack.imgur.com/XRKlU.jpg
My attempt was:
console.log(astar); // display array info
for (i=0; i < 50; i++){
for (j=0; j < 50; j++){
if( i in astar && j in astar[i] ){
abposx = getx(i);
abposy = gety(j);
ctx.fillStyle = "#000";
ctx.fillRect (abposx,abposy,10,10);
}
The idea is the “inner arrays” which have [0][1] positions im trying to see if “any” of them have [0] == i and [1] == j if so = true.
How should i alter it to work correctly & most efficiently – so that it will draw when it is found in the arrays
What you are currently doing is equivalent to
You may want:
Looping through all coordinates is very inefficient and would need a third loop for searching in astar. Better loop through astar and paint what you can get.
The correct three-loop code would have been:
(The some() loops through the array and returns true if an item matches the condition function. It equals the extended version posted by bfavaretto). Resulting in a performance of O(n^2.5)…