Need a specific item from an array


#1

hi there,

im pretty new to ionic and i am building a controll module but i need to select specific data from an array that i loaded in using an api

calling the data:

 <select placeholder="upload bestanden" class="styled-select semi-square" name="media" [(ngModel)]="content.upload_id">
            <option *ngFor="let upload of uploads" [value]="upload.id" >
              {{upload.name}}
            </option>
        </select><br> <hr>

the provider:

 public all() {
            return this.http.get<Answer>("answer");
          }

the page data:

{"id ": 1, name: "Intro", time_limit: null, module_id: 1, order: 1, can_go_back: 0, no_check: 0,…}
can_go_back: 0
contents: [{id: 1, title: null, description: null,…},…]
created_at: null
deleted_at: null
"id ": 1
module_id: 1
name: "Intro"
no_check: 0
order: 1
question_type_id: 1
time_limit: null
updated_at: null

the data i get:

0: {id: 1, text: "JA", correct: 0, description: "", question_id: 3, next: 4}
correct: 0
description: ""
id: 1
next: 4
question_id: 3
text: "JA"
1: {id: 2, text: "NEE", correct: 1, description: "", question_id: 3, next: 4}
2: {id: 3, text: "JA", correct: 0, description: "", question_id: 6, next: 7}
3: {id: 4, text: "NEE", correct: 1, description: "", question_id: 6, next: 9}
4: {id: 5, text: "JA", correct: 0, description: "", question_id: 9, next: 7}
5: {id: 6, text: "NEE", correct: 1, description: "", question_id: 9, next: 10}
6: {id: 7, text: "JA", correct: 0, description: "Dit is het foute antwoord, omdat...", question_id: 27,…}
7: {id: 8, text: "NEE", correct: 1, description: "Dit is het juiste antwoord!", question_id: 27, next: 28}
8: {id: 9, text: "Nooit", correct: 0, description: "Dit is het foute antwoord, omdat...", question_id: 28,…}
9: {id: 10, text: "Soms", correct: 0, description: "Dit is het foute antwoord, omdat...", question_id: 28,…}
10: {id: 11, text: "Vaak", correct: 1, description: "Dit is het juiste antwoord!", question_id: 28,…}

the data i need to get is when the page id == the question_id that i am able to pull that tabel to my form
it would be a real help if someone was able to shine a light on it for me


#2

@bobJL

you can use

Array.map to filter multiple items from an array and get what u need from that array.

ex:

 var obj = [
     {
         id: "1", name: "abc", value: "abc", country: "g"
         }, {
         id: "2", name: "def", value: "def", country: "h"
         }, {
         id: "3", name: "ghi", value: "ghi", country: "i"
     }
 ]
 
 const result = obj.map((key) => {
    const request = {
         id: key.id,
         name: key.name,
         country: key.country
     };
     return request;
});
 console.log(result);

#3

soo im able to fill in for example page id 3 and then it is able to pull every row where the question_id = the page id.
becouse thats what im trying to achieve


#4

and im using a database and api so i rekkon i need other statements than array handeling
i came up with this:

  public getAnswersForContent( id: number) {
        console.log("this:"+ id )
        return this.http.get<AnswersRequest>("answer/question/" + id);
      }

but it still needs some tweaking