1

I have this object:

public obj: any = { row1: [], row2: [], total: [], sumTotal: 0, count: 0 }; 

How can i create n empty arrays ?

Im trying something like this but its not working:

if (this.i === 0) { this.obj.row['_' + this.row] = new Array(); } 

Any suggestion?

6
  • Here you expect to create 3 arrays? Commented Sep 6, 2019 at 6:47
  • You probably want an array of arrays then. And you can use a loop to fill your array of arrays. Commented Sep 6, 2019 at 6:48
  • i expect to create n arrays...depend how many time i will be 0 . Commented Sep 6, 2019 at 6:49
  • i want to create new empty arrays with new name Commented Sep 6, 2019 at 6:49
  • on which base you decided the number of arrays? Commented Sep 6, 2019 at 6:49

2 Answers 2

1

if (this.i === 0) { this.obj.row['_' + this.row] = new Array(); }

You have to create the row property a little different. The way you typed it would try to access this.obj.row, in other words the row property of the obj property of this. And on that object, you would try to access the property '_' + this.row.

Below you find the correct solution. Just include the word row and omit the underscore:

if (this.i === 0) { this.obj['row' + this.row] = new Array(); } 
Sign up to request clarification or add additional context in comments.

Comments

0

You can create object like this

let length = 15;// Whatever you want for(let i=0;i<length;i++){ if(this.obj['row'+i] === undefined){ this.obj['row'+i] = []; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.