1

How can i get data from my response ( JSON object in array ) and assign to a variable in my component. This is how my results look like. But what i want to do is, i want to get only the price data from all the objects in the results and display in the GetData:Array

results

   Object 1

    comment : "Games on sale"
    Price:  100

    Object 2

   comment : "Animals on sale"
    Price:  139

    Object 3

    comment : "Table on sale"
    Price:  390

ts

     this.http.getGameData()
               .subscribe(data => {

                   this.ArrayLine = data;

               })

//array

public GetData:Array<any> = [
    {data: this.ArrayLine.price},

  ];

service

getGameData(){
        let headers = new Headers();
        let options = new RequestOptions({ headers: headers });
        return this.http.get('http://eg.com',options)
            .map((response:Response) => response.json());

    }
  • 5
    `this.ArrayLine = data.map((v)=>v.Price);` should do the trick – eko May 08 '17 at 08:35
  • @echonax i am plotting a graph against the values in {data: this.ArrayLine}. The graph wouldn't plot but when i fill manually like ArrayLine = [100,139, 390], the graph plots. What could i be doing wrong. Although when i console.log i see the prices –  May 08 '17 at 09:17
  • The reason for that is `getGameData()` method is an async method. Check: http://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular2 – eko May 08 '17 at 09:53

0 Answers0