Angular 2 change array value on subscribe












1















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 ?










share|improve this question

























  • 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 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











  • 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
















1















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 ?










share|improve this question

























  • 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 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











  • 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














1












1








1








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 ?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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 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











  • 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






  • 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











  • 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












3 Answers
3






active

oldest

votes


















4














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;
}





share|improve this answer
























  • Yes... Sometimes just dumb mistakes.. Sorry.

    – Andrėjus Lazauskas
    Jan 3 at 13:50



















2














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






share|improve this answer

































    1














    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!






    share|improve this answer
























      Your Answer






      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      4














      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;
      }





      share|improve this answer
























      • Yes... Sometimes just dumb mistakes.. Sorry.

        – Andrėjus Lazauskas
        Jan 3 at 13:50
















      4














      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;
      }





      share|improve this answer
























      • Yes... Sometimes just dumb mistakes.. Sorry.

        – Andrėjus Lazauskas
        Jan 3 at 13:50














      4












      4








      4







      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;
      }





      share|improve this answer













      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;
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      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



















      • 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













      2














      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






      share|improve this answer






























        2














        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






        share|improve this answer




























          2












          2








          2







          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






          share|improve this answer















          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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 3 at 16:04

























          answered Jan 3 at 14:15









          Roman LitvinovRoman Litvinov

          1819




          1819























              1














              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!






              share|improve this answer




























                1














                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!






                share|improve this answer


























                  1












                  1








                  1







                  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!






                  share|improve this answer













                  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!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 14:10









                  Tushar WalzadeTushar Walzade

                  2,28131935




                  2,28131935






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas