0

I have a web service call which returns an array of values. I want to store some values from the response to array.

Component.ts

     this.data.post_data('get/data',this.dataObj,true)
     .subscribe((data:any) => { 
         this.response= data.data;

      });

Webservice response

    0: {id: 785533, name: "UK ", avg: 0.6154,…}
    1: {id: 785533, name: "Usd ", avg: 0.698,…}
    2: {id: 785533, name: "ff ", avg: 50.61598,…}
    3: {id: 785533, name: "yy ", avg: 80.61198,…}
    4: {id: 785533, name: "nn ", avg: 10.618,…}
    5: {id: 785533, name: "mh ", avg: 0.6154898,…}
    6: {id: 785533, name: "tr ", avg: 70.615482198,…}
    7: {id: 785533, name: "es ", avg: 0.61548,…}

I need to save only 'name' and 'avg' values to array.

2
  • what is your expected output? Commented Oct 12, 2018 at 18:29
  • same as response array with only needed values Commented Oct 12, 2018 at 18:32

3 Answers 3

5

You can use the map method to generate a new array with the selected fields:

let result = this.response.map(x => { 
  return { name: x.name, avg: x.avg };
});
Sign up to request clarification or add additional context in comments.

Comments

2
    this.response = [];

    this.data.post_data('get/data',this.dataObj,true)
     .pipe(map(result => { return { "name": result.name, "avg": result.avg } } ))
     .subscribe((data:any) => { 
         this.response.push(data);
      });

Comments

0

If you have the response from the service, you can use it as you like. If you wish to create new array with name and avg values only you can do so using many ways. You can simply iterate through the response array and assign only the desired properties to the new object of the targeted Array.

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.