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);

2 Answers 2

1

To get an array, replace

groupID: any[];

With

groupID: any[] = [];

Then, also replace

this.groupID = element.GroupID;

With

this.groupID.push(element.GroupID);
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to add only if element.GroupID does not already exist in array?
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);
         }
     }

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.