0

I would like to create an empty 2D array. I know the size of the array, this gets dynamically decided. After the creation I want to insert different DOM objects inside this 2D array on dynamically created arraylocations. It says cannot set property of undefined. Did I do something wrong with the array initialisation?

let gridArrayNumbers = [];
      section.Layout.forEach(layout => {
        gridArrayNumbers.push(layout);//finallyhis , tarray will contain {2,3}
      });
      let gridArray: any [][] = [];

...

let e = document.createElement("label");
            e.textContent = element.content;             
            gridArray[element.layout.row][element.layout.column] = e;//throws the error here of undefined
4
  • can u paste the full error message ? Commented Jan 13, 2018 at 12:10
  • Unless this example is missing some code, gridArray is an empty array, so gridArray[element.layout.row][element.layout.column] isn't going to work Commented Jan 13, 2018 at 12:14
  • Is there a way to add elements on chosen positions like you said that won't work? Like initializing the array on some kind of way? I know the length for both 2D arrays but that did not help either... @user184994 Commented Jan 13, 2018 at 12:24
  • core.js:1350 ERROR Error: Uncaught (in promise): TypeError: Cannot set property '0' of undefined, the 0 property is a value that I know. @VikhyathMaiya Commented Jan 13, 2018 at 12:24

1 Answer 1

2

Where you're doing

gridArray[element.layout.row][element.layout.column] = e

Won't work because the array is empty. You need to initialize the values first:

gridArray[element.layout.row] = []
gridArray[element.layout.row][element.layout.column] = e
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.