0

I have json array - "tests" inside one of the keys is "GroupID" I wish to take all the "GroupID" and create new array that will include it (if possible as map - so no duplicated) I tried the following, but groupID contains only 1 value I tried .push, [], and many other ways and always getting errors how can I do it?

...
 groupID: any[];
...
 for (var index = 0; index < this.tests.length; index++) {
     var element = this.tests[index];
     this.groupID = element.GroupID;
    }

      console.log(this.groupID);
Shay Binyat
  • 115
  • 1
  • 1
  • 9

2 Answers2

1

To get an array, replace

groupID: any[];

With

groupID: any[] = [];

Then, also replace

this.groupID = element.GroupID;

With

this.groupID.push(element.GroupID);
BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
0

[SOLVED] thanks also to beetleJuice

added only if not existing in array:

...
 groupID: any[] = [];
...
    for (var index = 0; index < this.tests.length; index++) {
         var element = this.tests[index];
         if (this.groupID.indexOf(element.GroupID) == -1 ){
          this.groupID.push(element.GroupID);
         }
     }
Shay Binyat
  • 115
  • 1
  • 1
  • 9