Hello I am programming chess board but components aren’t visible.
I cannot see those div’s "{square.id}" in this line
const horizontalLine = ["1", "2", "3", "4", "5", "6", "7", "8"];
const verticalLine = ["a", "b", "c", "d", "e", "f", "g", "h"];
const squares = [verticalLine.map((verticals) => (
horizontalLine.map((horizontals) => (
{
id: `${verticals+horizontals}`,
ver: `${verticals}`,
hor: `${horizontals}`
}
))
))];
function App() {
return (
<div className='App'>
{squares.map((square) => (
<div id={square.id} key={square.id}>{square.id}</div>
))}
</div>
);
}
export default App;
2
Answers
squares
is an array of arrays of arrays and you are actually using only one loop. I would suggest to get rid of one wrapping array and add an additional inner loop.https://codesandbox.io/s/practical-dawn-q5jjpm?file=/src/App.js
Your
squares
object is an array with single element which is 8×8 2-dimensional matrix. When rendering thesquares
matrix, you are looping through only that single element. You need to access to first elementsquares
array and then loop through both rows and then columns: