#How to store a grid into a JavaScript array inside an object

10 messages · Page 1 of 1 (latest)

onyx saddle
#

you could also select them all at once using Array.from(document.querySelectorAll('.grid'))

#

no, like boardArray = Array.from(…)

#

or even shorter:

const gameBoard = ((player, computer, boardArray) => ({
  boardArray: Array.from(document.querySelectorAll(".grid"))
})();
#

(not sure why you need the parameters)

#

if your elements are not being selected, make sure your DOM is fully loaded at the time the JS runs

#

moreso, you don't need to wrap the code into an IIFE

#
const gameBoard = {
  boardArray: Array.from(document.querySelectorAll(".grid"))
}

without the return i cant access the boardArray outside the function
the return was still there, just implicit. When you have an arrow function that is comprised of exactly one expression, that expression is implicitly returned if you remove the braces.

#
function foo() {
  return 42
}

is (functionally) the same as

const foo = () => 42

which is exactly the same as

const foo = () => {
  return 42
}
#

then your DOM is not loaded