0

I am trying to create 40 cells with react , each should have a different title. For example a0 ...a9, b0 ..b9 etc. This is my code

const mapItems = () => {
    let playerId = ["a", "b", "c", "d"];
    let cellId;
    let l, j;
    for (j = 0; j < 4; j++) {
        for (l = 0; l < 10; l++) {
            cellId = playerId[j].concat(l);
            return (
                    <Cell title={cellId} className={"column"}>
                        {returnItemsForCell(cellId)}
                    </Cell>
            )
        }
    }

}

For some reason I get an error that a variable is not initialized , and only one in nested For loop.
Any ideas on how to do it right? Thanks in advance

2
  • 1
    Your nested loop looks correct may be your editor showing it wrongly, but you will get only 4 results in this code. 40 items will not be there on the list! because you are returning instantly on second loop Commented Sep 16, 2021 at 10:32
  • You can use snippets for such questions Commented Sep 16, 2021 at 12:01

1 Answer 1

-1

If someone ever has the same problem , this is the solution.

const mapItems = () => {
    let playerId = ["a", "b", "c", "d"];
    let cellId;
    let cellsList = [];
    for (let j = 0; j < 4; j++) {
        for (let l = 0; l < 10; l++) {
            cellId = playerId[j].concat(l);

            cellsList.push(
                <Cell title={cellId} className={"column"}>
                    {returnItemsForCell(cellId)}
                </Cell>
            )
        }
    }
    return cellsList;

The return just jumped out of the loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.