Fill array with array return

Asked

Viewed 71 times

-3

I make a request, where search data of a request, where it returns me the "Areas" and saved inside an array. I make an iterator to search the "topics" referring to that "Area", I wanted to put this return, within the same array, but with a new position, with the data referring to the "Areas"

Return of the first request inserir a descrição da imagem aqui Return requests that are generated from the first request inserir a descrição da imagem aqui

The goal is to place these second data within the first Array

Something like that would come up

id: 1
parent_id: 0
product_id: 1
name: "Clínica Médica"
photo: "http://simsave.s3-website.us-east-2.amazonaws.com/public/topics/{size}/1565627445064.jpg"
description: null
type: [""]
medicine: 1
nursing: 0
nursing_tech: 0 
"dados":Array([0..9]) // Aqui com os valores do primeiro retorno


this.api.Get("../topic/product/listProducts?product_id=" + id).subscribe((res: any) => {
      let topics = [res.length];

        for(let i:number = 0; i < res.length; i++){
          topics[i] = res[i];
          this.api.Get("../topic/parent/listProductParent?parent_id=" + topics[i].id).subscribe((ret: any) => {
              topics[i].push(ret);
          });
        }
      console.log(topics);
    });
  • please enter the result of the request, we do not have access to this API, so we cannot write a response about something we do not know.

  • I just put it here @Paz

  • 1
  • If I understand correctly, you make a request that returns an array of objects. Inside this object you want to use the id of each one to pick up other objects that contain the property parent_id equal to the id of these objects. And inside the "parent" object you want to fill a given property with the objects of the second request that have parent_id equal to id.

  • Yeah, that’s confusing but that’s it

  • @Peace is just that

  • in fact, no and well that you answered, we will open a chat?

  • can open the chat...

  • all right, I’ll open

  • I opened a room but couldn’t add you @Paz

  • pass the link please

Show 7 more comments

1 answer

2


Here’s an example of how to implement in your case, you should use for to put your children’s data inside the property dados while iteration occurs (untested as I do not have access to your API).

var chamada_1;

this.api.Get("../topic/product/listProducts?product_id=" + id).subscribe((res: any) => {
      let topics = [res.length];
      chamada_1 = res;
        for(let i:number = 0; i < res.length; i++){
          topics[i] = res[i];
          this.api.Get("../topic/parent/listProductParent?parent_id=" + topics[i].id).subscribe((ret: any) => {
              chamada_1[i]["dados"] = ret; //aqui insiro os dados que tem o parent_id correspondente com o id do pai
              
          });
        }
      console.log(chamada_1);
    });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.