#How to store a grid into a JavaScript array inside an object
10 messages · Page 1 of 1 (latest)
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
thereturnwas 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
if you load the script from another file (as you should), then you can use defer: https://javascript.info/script-async-defer