0

In service call i get a below response Currently i have foreach the collections and push the productID and ProductName in id and name into the options array and bind into html. Need to replace this with spread opreator with destructive object.

[

{productID:123,ProductName:'laptop',Qnty:2},

{productID:12,ProductName:'mobile',Qnty:2},

{productID:33,ProductName:'other accessories',Qnty:9},

{productID:53,ProductName:'Tv',Qnty:2},


]

//service call function block

 let options =[];
    console.log(response);
        response.forEach(value =>{
          this.options.push({
            id:value.productID,
            name: value.ProductName
           });

        });

I have trying to destructure the object.

let {productID:id,ProductName:name} = response;

But i don't know i how to insert this into options array.

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Mohamed Sahir
  • 2,482
  • 8
  • 40
  • 71
  • 1
    "*Need to replace this with spread and destructive operator.*" - why? Your code is working fine. And how have you tried to use them? – Bergi Aug 31 '18 at 20:55
  • 3
    There are no "spread and destructive" operators. There's spread syntax, and there's destructuring. You might be able to apply destructuring here, but it's not really worth it. First you should look into the `map` function - avoid `forEach`. Also `this.options = ` inside a loop hardly makes sense. – Bergi Aug 31 '18 at 20:56
  • @bergi thanks for the comments, i thought spread and destructive object will reduce the code and performance wise, ok thank you i will go for map instead of foreach – Mohamed Sahir Aug 31 '18 at 21:02

0 Answers0