Angular 2 change array value on subscribe
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 ?
javascript arrays angular typescript
add a comment |
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 ?
javascript arrays angular typescript
Can you add yourhtml
code?
– veben
Jan 3 at 13:46
2
Your example doesn't show you aren't updating the price. By usinglet
you are creating a new variable and aren't assigning it to anything. Changing the value would be done by doing something likeentry.price = entry.price * 1.10837;
– peinearydevelopment
Jan 3 at 13:47
Sure, will edit right now.
– Andrėjus Lazauskas
Jan 3 at 13:47
Oh... Sorry for dumb question, really messed up... ;D
– Andrėjus Lazauskas
Jan 3 at 13:49
As @AndrėjusLazauskas said, you are just changing it locally.
– Amit Chigadani
Jan 3 at 13:49
add a comment |
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 ?
javascript arrays angular typescript
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 ?
javascript arrays angular typescript
javascript arrays angular typescript
edited Jan 3 at 14:08
Tushar Walzade
2,28131935
2,28131935
asked Jan 3 at 13:44
Andrėjus LazauskasAndrėjus Lazauskas
437
437
Can you add yourhtml
code?
– veben
Jan 3 at 13:46
2
Your example doesn't show you aren't updating the price. By usinglet
you are creating a new variable and aren't assigning it to anything. Changing the value would be done by doing something likeentry.price = entry.price * 1.10837;
– peinearydevelopment
Jan 3 at 13:47
Sure, will edit right now.
– Andrėjus Lazauskas
Jan 3 at 13:47
Oh... Sorry for dumb question, really messed up... ;D
– Andrėjus Lazauskas
Jan 3 at 13:49
As @AndrėjusLazauskas said, you are just changing it locally.
– Amit Chigadani
Jan 3 at 13:49
add a comment |
Can you add yourhtml
code?
– veben
Jan 3 at 13:46
2
Your example doesn't show you aren't updating the price. By usinglet
you are creating a new variable and aren't assigning it to anything. Changing the value would be done by doing something likeentry.price = entry.price * 1.10837;
– peinearydevelopment
Jan 3 at 13:47
Sure, will edit right now.
– Andrėjus Lazauskas
Jan 3 at 13:47
Oh... Sorry for dumb question, really messed up... ;D
– Andrėjus Lazauskas
Jan 3 at 13:49
As @AndrėjusLazauskas said, you are just changing it locally.
– Amit Chigadani
Jan 3 at 13:49
Can you add your
html
code?– veben
Jan 3 at 13:46
Can you add your
html
code?– veben
Jan 3 at 13:46
2
2
Your example doesn't show you aren't updating the price. By using
let
you are creating a new variable and aren't assigning it to anything. Changing the value would be done by doing something like entry.price = entry.price * 1.10837;
– peinearydevelopment
Jan 3 at 13:47
Your example doesn't show you aren't updating the price. By using
let
you are creating a new variable and aren't assigning it to anything. Changing the value would be done by doing something like entry.price = entry.price * 1.10837;
– peinearydevelopment
Jan 3 at 13:47
Sure, will edit right now.
– Andrėjus Lazauskas
Jan 3 at 13:47
Sure, will edit right now.
– Andrėjus Lazauskas
Jan 3 at 13:47
Oh... Sorry for dumb question, really messed up... ;D
– Andrėjus Lazauskas
Jan 3 at 13:49
Oh... Sorry for dumb question, really messed up... ;D
– Andrėjus Lazauskas
Jan 3 at 13:49
As @AndrėjusLazauskas said, you are just changing it locally.
– Amit Chigadani
Jan 3 at 13:49
As @AndrėjusLazauskas said, you are just changing it locally.
– Amit Chigadani
Jan 3 at 13:49
add a comment |
3 Answers
3
active
oldest
votes
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;
}
Yes... Sometimes just dumb mistakes.. Sorry.
– Andrėjus Lazauskas
Jan 3 at 13:50
add a comment |
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
add a comment |
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!
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023517%2fangular-2-change-array-value-on-subscribe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;
}
Yes... Sometimes just dumb mistakes.. Sorry.
– Andrėjus Lazauskas
Jan 3 at 13:50
add a comment |
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;
}
Yes... Sometimes just dumb mistakes.. Sorry.
– Andrėjus Lazauskas
Jan 3 at 13:50
add a comment |
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;
}
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;
}
answered Jan 3 at 13:48
B.BenjoB.Benjo
413311
413311
Yes... Sometimes just dumb mistakes.. Sorry.
– Andrėjus Lazauskas
Jan 3 at 13:50
add a comment |
Yes... Sometimes just dumb mistakes.. Sorry.
– Andrėjus Lazauskas
Jan 3 at 13:50
Yes... Sometimes just dumb mistakes.. Sorry.
– Andrėjus Lazauskas
Jan 3 at 13:50
Yes... Sometimes just dumb mistakes.. Sorry.
– Andrėjus Lazauskas
Jan 3 at 13:50
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Jan 3 at 16:04
answered Jan 3 at 14:15
Roman LitvinovRoman Litvinov
1819
1819
add a comment |
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
answered Jan 3 at 14:10
Tushar WalzadeTushar Walzade
2,28131935
2,28131935
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023517%2fangular-2-change-array-value-on-subscribe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you add your
html
code?– veben
Jan 3 at 13:46
2
Your example doesn't show you aren't updating the price. By using
let
you are creating a new variable and aren't assigning it to anything. Changing the value would be done by doing something likeentry.price = entry.price * 1.10837;
– peinearydevelopment
Jan 3 at 13:47
Sure, will edit right now.
– Andrėjus Lazauskas
Jan 3 at 13:47
Oh... Sorry for dumb question, really messed up... ;D
– Andrėjus Lazauskas
Jan 3 at 13:49
As @AndrėjusLazauskas said, you are just changing it locally.
– Amit Chigadani
Jan 3 at 13:49