Re-assign value inside complex object during runtime in Angular 6












-1















I currently work with angular 6 and have this method:



 public Save(): void {
this.data.station.parkingSlot.forEach(p => {
if(p.isAvailable){
p.isAvailable = false;
this._carService.addCar(this.data);
return true;
}else{
alert("all parking lots are taken")
}
})
}


I want to send data (this.data) with updated isAvailable to false as parameter inside this._carService.addCar(this.data).



Below, you can see how this.data object looks like. As you can see, the last property isAvailable from parkingSlot is not updated to false as it should be.



{
"id": 3,
"carType": "Spinter",
"plateNumber": "BUS-XXX-XXX",
"station": {
"id": 2,
"name": "STATION_2",
"longitude": 3,
"latitude": 4,
"city": "Oslo",
"streetName": "street1",
"houseNumber": 55,
"zipCode": 31000,
"parkingSlot": [
{
"id": 1,
"name": "PARKING_1",
"isAvailable": false
},
{
"id": 2,
"name": "PARKING_2",
"isAvailable": false
},
{
"id": 3,
"name": "PARKING_3",
**"isAvailable": true**
}
]
}
}


and I want that it looks like:



{
"id": 3,
"carType": "Spinter",
"plateNumber": "BUS-XXX-XXX",
"station": {
"id": 2,
"name": "STATION_2",
"longitude": 3,
"latitude": 4,
"city": "Oslo",
"streetName": "street1",
"houseNumber": 55,
"zipCode": 31000,
"parkingSlot": [
{
"id": 1,
"name": "PARKING_1",
"isAvailable": false
},
{
"id": 2,
"name": "PARKING_2",
"isAvailable": false
},
{
"id": 3,
"name": "PARKING_3",
**"isAvailable": false**
}
]
}
}


Does anyone know what is the solution?I try everything I found while Googling.
Thanks a lot!



Also I










share|improve this question




















  • 2





    If the p.isAvailable is true, are you wanting to set it to false? In you code it looks like you are setting it to true if it is already true (if(p.isAvailable){ p.isAvailable = true; ... })

    – Daniel W Strimpel
    Nov 26 '18 at 19:51











  • what's the condition for updating isAvailable to true? if you want to make it false do if(p.isAvailable){ p.isAvailable = false;

    – Code-EZ
    Nov 26 '18 at 19:54













  • I added p.isAvailable = false but it's value inside this. data object is still unchanged

    – Yuniku_123
    Nov 26 '18 at 20:00











  • @Yuniku_123 See this stackblitz.com/edit/rxjs-djayls?file=index.ts

    – Code-EZ
    Nov 26 '18 at 20:09
















-1















I currently work with angular 6 and have this method:



 public Save(): void {
this.data.station.parkingSlot.forEach(p => {
if(p.isAvailable){
p.isAvailable = false;
this._carService.addCar(this.data);
return true;
}else{
alert("all parking lots are taken")
}
})
}


I want to send data (this.data) with updated isAvailable to false as parameter inside this._carService.addCar(this.data).



Below, you can see how this.data object looks like. As you can see, the last property isAvailable from parkingSlot is not updated to false as it should be.



{
"id": 3,
"carType": "Spinter",
"plateNumber": "BUS-XXX-XXX",
"station": {
"id": 2,
"name": "STATION_2",
"longitude": 3,
"latitude": 4,
"city": "Oslo",
"streetName": "street1",
"houseNumber": 55,
"zipCode": 31000,
"parkingSlot": [
{
"id": 1,
"name": "PARKING_1",
"isAvailable": false
},
{
"id": 2,
"name": "PARKING_2",
"isAvailable": false
},
{
"id": 3,
"name": "PARKING_3",
**"isAvailable": true**
}
]
}
}


and I want that it looks like:



{
"id": 3,
"carType": "Spinter",
"plateNumber": "BUS-XXX-XXX",
"station": {
"id": 2,
"name": "STATION_2",
"longitude": 3,
"latitude": 4,
"city": "Oslo",
"streetName": "street1",
"houseNumber": 55,
"zipCode": 31000,
"parkingSlot": [
{
"id": 1,
"name": "PARKING_1",
"isAvailable": false
},
{
"id": 2,
"name": "PARKING_2",
"isAvailable": false
},
{
"id": 3,
"name": "PARKING_3",
**"isAvailable": false**
}
]
}
}


Does anyone know what is the solution?I try everything I found while Googling.
Thanks a lot!



Also I










share|improve this question




















  • 2





    If the p.isAvailable is true, are you wanting to set it to false? In you code it looks like you are setting it to true if it is already true (if(p.isAvailable){ p.isAvailable = true; ... })

    – Daniel W Strimpel
    Nov 26 '18 at 19:51











  • what's the condition for updating isAvailable to true? if you want to make it false do if(p.isAvailable){ p.isAvailable = false;

    – Code-EZ
    Nov 26 '18 at 19:54













  • I added p.isAvailable = false but it's value inside this. data object is still unchanged

    – Yuniku_123
    Nov 26 '18 at 20:00











  • @Yuniku_123 See this stackblitz.com/edit/rxjs-djayls?file=index.ts

    – Code-EZ
    Nov 26 '18 at 20:09














-1












-1








-1








I currently work with angular 6 and have this method:



 public Save(): void {
this.data.station.parkingSlot.forEach(p => {
if(p.isAvailable){
p.isAvailable = false;
this._carService.addCar(this.data);
return true;
}else{
alert("all parking lots are taken")
}
})
}


I want to send data (this.data) with updated isAvailable to false as parameter inside this._carService.addCar(this.data).



Below, you can see how this.data object looks like. As you can see, the last property isAvailable from parkingSlot is not updated to false as it should be.



{
"id": 3,
"carType": "Spinter",
"plateNumber": "BUS-XXX-XXX",
"station": {
"id": 2,
"name": "STATION_2",
"longitude": 3,
"latitude": 4,
"city": "Oslo",
"streetName": "street1",
"houseNumber": 55,
"zipCode": 31000,
"parkingSlot": [
{
"id": 1,
"name": "PARKING_1",
"isAvailable": false
},
{
"id": 2,
"name": "PARKING_2",
"isAvailable": false
},
{
"id": 3,
"name": "PARKING_3",
**"isAvailable": true**
}
]
}
}


and I want that it looks like:



{
"id": 3,
"carType": "Spinter",
"plateNumber": "BUS-XXX-XXX",
"station": {
"id": 2,
"name": "STATION_2",
"longitude": 3,
"latitude": 4,
"city": "Oslo",
"streetName": "street1",
"houseNumber": 55,
"zipCode": 31000,
"parkingSlot": [
{
"id": 1,
"name": "PARKING_1",
"isAvailable": false
},
{
"id": 2,
"name": "PARKING_2",
"isAvailable": false
},
{
"id": 3,
"name": "PARKING_3",
**"isAvailable": false**
}
]
}
}


Does anyone know what is the solution?I try everything I found while Googling.
Thanks a lot!



Also I










share|improve this question
















I currently work with angular 6 and have this method:



 public Save(): void {
this.data.station.parkingSlot.forEach(p => {
if(p.isAvailable){
p.isAvailable = false;
this._carService.addCar(this.data);
return true;
}else{
alert("all parking lots are taken")
}
})
}


I want to send data (this.data) with updated isAvailable to false as parameter inside this._carService.addCar(this.data).



Below, you can see how this.data object looks like. As you can see, the last property isAvailable from parkingSlot is not updated to false as it should be.



{
"id": 3,
"carType": "Spinter",
"plateNumber": "BUS-XXX-XXX",
"station": {
"id": 2,
"name": "STATION_2",
"longitude": 3,
"latitude": 4,
"city": "Oslo",
"streetName": "street1",
"houseNumber": 55,
"zipCode": 31000,
"parkingSlot": [
{
"id": 1,
"name": "PARKING_1",
"isAvailable": false
},
{
"id": 2,
"name": "PARKING_2",
"isAvailable": false
},
{
"id": 3,
"name": "PARKING_3",
**"isAvailable": true**
}
]
}
}


and I want that it looks like:



{
"id": 3,
"carType": "Spinter",
"plateNumber": "BUS-XXX-XXX",
"station": {
"id": 2,
"name": "STATION_2",
"longitude": 3,
"latitude": 4,
"city": "Oslo",
"streetName": "street1",
"houseNumber": 55,
"zipCode": 31000,
"parkingSlot": [
{
"id": 1,
"name": "PARKING_1",
"isAvailable": false
},
{
"id": 2,
"name": "PARKING_2",
"isAvailable": false
},
{
"id": 3,
"name": "PARKING_3",
**"isAvailable": false**
}
]
}
}


Does anyone know what is the solution?I try everything I found while Googling.
Thanks a lot!



Also I







javascript angular typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 19:59







Yuniku_123

















asked Nov 26 '18 at 19:47









Yuniku_123Yuniku_123

1041214




1041214








  • 2





    If the p.isAvailable is true, are you wanting to set it to false? In you code it looks like you are setting it to true if it is already true (if(p.isAvailable){ p.isAvailable = true; ... })

    – Daniel W Strimpel
    Nov 26 '18 at 19:51











  • what's the condition for updating isAvailable to true? if you want to make it false do if(p.isAvailable){ p.isAvailable = false;

    – Code-EZ
    Nov 26 '18 at 19:54













  • I added p.isAvailable = false but it's value inside this. data object is still unchanged

    – Yuniku_123
    Nov 26 '18 at 20:00











  • @Yuniku_123 See this stackblitz.com/edit/rxjs-djayls?file=index.ts

    – Code-EZ
    Nov 26 '18 at 20:09














  • 2





    If the p.isAvailable is true, are you wanting to set it to false? In you code it looks like you are setting it to true if it is already true (if(p.isAvailable){ p.isAvailable = true; ... })

    – Daniel W Strimpel
    Nov 26 '18 at 19:51











  • what's the condition for updating isAvailable to true? if you want to make it false do if(p.isAvailable){ p.isAvailable = false;

    – Code-EZ
    Nov 26 '18 at 19:54













  • I added p.isAvailable = false but it's value inside this. data object is still unchanged

    – Yuniku_123
    Nov 26 '18 at 20:00











  • @Yuniku_123 See this stackblitz.com/edit/rxjs-djayls?file=index.ts

    – Code-EZ
    Nov 26 '18 at 20:09








2




2





If the p.isAvailable is true, are you wanting to set it to false? In you code it looks like you are setting it to true if it is already true (if(p.isAvailable){ p.isAvailable = true; ... })

– Daniel W Strimpel
Nov 26 '18 at 19:51





If the p.isAvailable is true, are you wanting to set it to false? In you code it looks like you are setting it to true if it is already true (if(p.isAvailable){ p.isAvailable = true; ... })

– Daniel W Strimpel
Nov 26 '18 at 19:51













what's the condition for updating isAvailable to true? if you want to make it false do if(p.isAvailable){ p.isAvailable = false;

– Code-EZ
Nov 26 '18 at 19:54







what's the condition for updating isAvailable to true? if you want to make it false do if(p.isAvailable){ p.isAvailable = false;

– Code-EZ
Nov 26 '18 at 19:54















I added p.isAvailable = false but it's value inside this. data object is still unchanged

– Yuniku_123
Nov 26 '18 at 20:00





I added p.isAvailable = false but it's value inside this. data object is still unchanged

– Yuniku_123
Nov 26 '18 at 20:00













@Yuniku_123 See this stackblitz.com/edit/rxjs-djayls?file=index.ts

– Code-EZ
Nov 26 '18 at 20:09





@Yuniku_123 See this stackblitz.com/edit/rxjs-djayls?file=index.ts

– Code-EZ
Nov 26 '18 at 20:09












2 Answers
2






active

oldest

votes


















1














Try this! The problem is that you are adding to this.data too early. Add to the array when all the information you want has changed :)



public Save(): void {
this.data.station.parkingSlot.forEach(p => {
if(p.isAvailable){
p.isAvailable = false;
return true;
} else {
alert("all parking lots are taken")
}
});
this._carService.addCar(this.data); // puts complete data in the this.data
}





share|improve this answer































    -2














    The issue is you are calling the addCar in each iteration however you might want to execute once all the flag is set to false.



     public Save(): void {
    this.data.station.parkingSlot.forEach(p => {
    if(p.isAvailable){
    p.isAvailable = false;
    return true;
    }else{
    alert("all parking lots are taken")
    }
    })
    this._carService.addCar(this.data); // execute after all the isAvailable is set to false.
    }





    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%2f53488001%2fre-assign-value-inside-complex-object-during-runtime-in-angular-6%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Try this! The problem is that you are adding to this.data too early. Add to the array when all the information you want has changed :)



      public Save(): void {
      this.data.station.parkingSlot.forEach(p => {
      if(p.isAvailable){
      p.isAvailable = false;
      return true;
      } else {
      alert("all parking lots are taken")
      }
      });
      this._carService.addCar(this.data); // puts complete data in the this.data
      }





      share|improve this answer




























        1














        Try this! The problem is that you are adding to this.data too early. Add to the array when all the information you want has changed :)



        public Save(): void {
        this.data.station.parkingSlot.forEach(p => {
        if(p.isAvailable){
        p.isAvailable = false;
        return true;
        } else {
        alert("all parking lots are taken")
        }
        });
        this._carService.addCar(this.data); // puts complete data in the this.data
        }





        share|improve this answer


























          1












          1








          1







          Try this! The problem is that you are adding to this.data too early. Add to the array when all the information you want has changed :)



          public Save(): void {
          this.data.station.parkingSlot.forEach(p => {
          if(p.isAvailable){
          p.isAvailable = false;
          return true;
          } else {
          alert("all parking lots are taken")
          }
          });
          this._carService.addCar(this.data); // puts complete data in the this.data
          }





          share|improve this answer













          Try this! The problem is that you are adding to this.data too early. Add to the array when all the information you want has changed :)



          public Save(): void {
          this.data.station.parkingSlot.forEach(p => {
          if(p.isAvailable){
          p.isAvailable = false;
          return true;
          } else {
          alert("all parking lots are taken")
          }
          });
          this._carService.addCar(this.data); // puts complete data in the this.data
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 20:09









          gatsbyzgatsbyz

          220317




          220317

























              -2














              The issue is you are calling the addCar in each iteration however you might want to execute once all the flag is set to false.



               public Save(): void {
              this.data.station.parkingSlot.forEach(p => {
              if(p.isAvailable){
              p.isAvailable = false;
              return true;
              }else{
              alert("all parking lots are taken")
              }
              })
              this._carService.addCar(this.data); // execute after all the isAvailable is set to false.
              }





              share|improve this answer




























                -2














                The issue is you are calling the addCar in each iteration however you might want to execute once all the flag is set to false.



                 public Save(): void {
                this.data.station.parkingSlot.forEach(p => {
                if(p.isAvailable){
                p.isAvailable = false;
                return true;
                }else{
                alert("all parking lots are taken")
                }
                })
                this._carService.addCar(this.data); // execute after all the isAvailable is set to false.
                }





                share|improve this answer


























                  -2












                  -2








                  -2







                  The issue is you are calling the addCar in each iteration however you might want to execute once all the flag is set to false.



                   public Save(): void {
                  this.data.station.parkingSlot.forEach(p => {
                  if(p.isAvailable){
                  p.isAvailable = false;
                  return true;
                  }else{
                  alert("all parking lots are taken")
                  }
                  })
                  this._carService.addCar(this.data); // execute after all the isAvailable is set to false.
                  }





                  share|improve this answer













                  The issue is you are calling the addCar in each iteration however you might want to execute once all the flag is set to false.



                   public Save(): void {
                  this.data.station.parkingSlot.forEach(p => {
                  if(p.isAvailable){
                  p.isAvailable = false;
                  return true;
                  }else{
                  alert("all parking lots are taken")
                  }
                  })
                  this._carService.addCar(this.data); // execute after all the isAvailable is set to false.
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 20:05









                  Sunil SinghSunil Singh

                  6,3372627




                  6,3372627






























                      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%2f53488001%2fre-assign-value-inside-complex-object-during-runtime-in-angular-6%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

                      Futebolista

                      Lallio

                      Jornalista