-1

I want to create a table with a boucle FOR in javascript.

But I push tables, so my final table is:

[ [{},{}],[{},{}] ]

but I would like:

[{},{}]

How can I delete [ ] in a table.

My code :

this.table = [];
for (let i = 0; i < numberUnicorn; i++) {
  this.subMember = this.itemService.getUnicornMembership().subscribe((items) => {
    this.table.push(items);
  });
}

Thank you

Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166
Sylvain
  • 238
  • 2
  • 15
  • 2
    Can you show your code to get the results mentioned? – ild flue Jan 26 '18 at 07:16
  • Do you mean table or array ? If array you can use `concat` to merge 2 arrays – DarknessZX Jan 26 '18 at 07:17
  • Assuming your table is stored in the variable `t`: If what you want is just `[{}, {}]` and your table (`t`) is `[ [{},{}],[{},{}] ]`, you'd have to [*clone*](https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript) (not directly reference) one of the rows (`[{}, {}]`) and set `t` to that value. If you want to delete one of the rows, use the `delete` keyword with the appropriate index. – Agi Hammerthief Jan 26 '18 at 07:18

1 Answers1

2

You can use concat to merge array

this.table = [];
    for (var i = 0; i < numberUnicorn; i++) {
        this.subMember = this.itemService.getUnicornMembership().subscribe(items => {
        this.table = this.table.concat(items);
    });

Or with ES6

this.table.push(...items)
DarknessZX
  • 686
  • 3
  • 12