Reputation: 356
I want to change some values in an array after I subscribe to it.
this.data = this.search.getProductsById(this.id).subscribe
((data: any) => {
this.list = data;
//Here for each price I want to change price value
for (let entry of this.list.deals.new) {
if (entry.country == 'UK') {
let p = entry.price * 1.10837; //here I change price
console.log(p); //displays new price
}
}
this.loading = true;
});
But in HTML
it displays old price, not p
. How to change it so I would get in html
new one ?
Upvotes: 2
Views: 1751
Reputation: 503
As I understand, your method getProductsById() return an array of deals which have country field. If I understand correctly, you should use map operator something like this
this.data = this.search.getProductsById(this.id)
.pipe(map((data: any) => {
if (data.deals.country === 'UK') {
return data.deals.price = data.deals.price * 1.10837;
}
}))
.subscribe(data => console.log(data.deals.price));
For better understanding give us the structure of the returned object
Upvotes: 2
Reputation: 3809
Your p
is local to your if
condition in a class only. You simply need to assign the value to the variable property itself.
So, just replace the line let p = entry.price * 1.10837;
with entry.price = entry.price * 1.10837;
That's it!
Upvotes: 1
Reputation: 573
I think it's because you didn't set the new value p
in your array, i think it's like this:
this.data = this.search.getProductsById(this.id).subscribe
((data: any) => {
this.list = data;
//Here for each price I want to change price value
for(let entry of this.list.deals.new){
if(entry.country == 'UK'){
entry.price *= 1.10837;
console.log(entry.price); //displays new price
}
}
this.loading = true;
}
Upvotes: 4