update object property without rewriting












0















I have a method which takes two parameters key for property name and value for the property value. And when I use it I want to update data[key] and add as many values there as I call the method setData. I need not rewrite value with the same key, but update it and add a new one, so the final object can look like this:



{
patients:
formValue: […],
filtersValue: {}
}


current realization:



const data = {}

setData(key, value) {
Object.assign(this.data[key] = {}, value);
}


setData(this.stateName, { firstItemsValue: this.itemsCopy });
setData(this.stateName, { secondItemsValue: this.itemsCopy });









share|improve this question




















  • 1





    what about data[key] = value?

    – Aprillion
    Nov 15 '18 at 8:28











  • Add an input and an output example of your method to make it more clear

    – Panos K
    Nov 15 '18 at 8:31











  • @Aprillion data[key] = value will rewrite it on every method call

    – Bogdan Tushevskyi
    Nov 15 '18 at 8:40
















0















I have a method which takes two parameters key for property name and value for the property value. And when I use it I want to update data[key] and add as many values there as I call the method setData. I need not rewrite value with the same key, but update it and add a new one, so the final object can look like this:



{
patients:
formValue: […],
filtersValue: {}
}


current realization:



const data = {}

setData(key, value) {
Object.assign(this.data[key] = {}, value);
}


setData(this.stateName, { firstItemsValue: this.itemsCopy });
setData(this.stateName, { secondItemsValue: this.itemsCopy });









share|improve this question




















  • 1





    what about data[key] = value?

    – Aprillion
    Nov 15 '18 at 8:28











  • Add an input and an output example of your method to make it more clear

    – Panos K
    Nov 15 '18 at 8:31











  • @Aprillion data[key] = value will rewrite it on every method call

    – Bogdan Tushevskyi
    Nov 15 '18 at 8:40














0












0








0








I have a method which takes two parameters key for property name and value for the property value. And when I use it I want to update data[key] and add as many values there as I call the method setData. I need not rewrite value with the same key, but update it and add a new one, so the final object can look like this:



{
patients:
formValue: […],
filtersValue: {}
}


current realization:



const data = {}

setData(key, value) {
Object.assign(this.data[key] = {}, value);
}


setData(this.stateName, { firstItemsValue: this.itemsCopy });
setData(this.stateName, { secondItemsValue: this.itemsCopy });









share|improve this question
















I have a method which takes two parameters key for property name and value for the property value. And when I use it I want to update data[key] and add as many values there as I call the method setData. I need not rewrite value with the same key, but update it and add a new one, so the final object can look like this:



{
patients:
formValue: […],
filtersValue: {}
}


current realization:



const data = {}

setData(key, value) {
Object.assign(this.data[key] = {}, value);
}


setData(this.stateName, { firstItemsValue: this.itemsCopy });
setData(this.stateName, { secondItemsValue: this.itemsCopy });






javascript object






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 9:51







Bogdan Tushevskyi

















asked Nov 15 '18 at 8:26









Bogdan TushevskyiBogdan Tushevskyi

14814




14814








  • 1





    what about data[key] = value?

    – Aprillion
    Nov 15 '18 at 8:28











  • Add an input and an output example of your method to make it more clear

    – Panos K
    Nov 15 '18 at 8:31











  • @Aprillion data[key] = value will rewrite it on every method call

    – Bogdan Tushevskyi
    Nov 15 '18 at 8:40














  • 1





    what about data[key] = value?

    – Aprillion
    Nov 15 '18 at 8:28











  • Add an input and an output example of your method to make it more clear

    – Panos K
    Nov 15 '18 at 8:31











  • @Aprillion data[key] = value will rewrite it on every method call

    – Bogdan Tushevskyi
    Nov 15 '18 at 8:40








1




1





what about data[key] = value?

– Aprillion
Nov 15 '18 at 8:28





what about data[key] = value?

– Aprillion
Nov 15 '18 at 8:28













Add an input and an output example of your method to make it more clear

– Panos K
Nov 15 '18 at 8:31





Add an input and an output example of your method to make it more clear

– Panos K
Nov 15 '18 at 8:31













@Aprillion data[key] = value will rewrite it on every method call

– Bogdan Tushevskyi
Nov 15 '18 at 8:40





@Aprillion data[key] = value will rewrite it on every method call

– Bogdan Tushevskyi
Nov 15 '18 at 8:40












5 Answers
5






active

oldest

votes


















1














The solution is:



setData(key, value) {
this.data[key] = { ...this.data[key], ...value };
}





share|improve this answer
























  • spread operator? why not ... ?

    – Tornike Shavishvili
    Nov 15 '18 at 15:29











  • @TornikeShavishvili what is the difference between the spread operator and '...'?

    – Bogdan Tushevskyi
    Nov 16 '18 at 8:42











  • In your case three dots are spread operator. In javascript three dots '...' have basically two meanings, 1. If used in function declaration it defines "rest prameters", please see following doxumentations: Functions Rest Parameters 2. In other cases it is interpreted as spread operator. Please see Expressions - Spread syntax

    – Tornike Shavishvili
    Nov 16 '18 at 8:52













  • @TornikeShavishvili yes, I know this, I don't understand your first question

    – Bogdan Tushevskyi
    Nov 16 '18 at 8:54











  • I think my comment confused you. I meant that spread operator is good to use. I have to be more clear next time.

    – Tornike Shavishvili
    Nov 16 '18 at 8:55



















0














You have mentioned that you want to add as many values as youi call setData. That sounds like a job for array.



You could use follwoing code:



function setData(targetObj, propName, valueToAdd){

if(typeof targetObj[propName] === 'undefined'){
targetObj[propName] = ;
}
if(Array.isArray(targetObj[propName])){
targetObj[propName].push(valueToAdd);
}else{
console.log('property '+propName +' already exists and is not an array');
}
}





share|improve this answer































    0














    Use like this:



    const data = {}

    setData(key, value) {
    Object.assign(data, {[key]: value});
    }


    BTW, You can simply set the data like:



    setData(key, value) {
    data[key] = value
    }





    share|improve this answer





















    • 1





      Is there a reason for Object.assign over plain bracket notation? (also, key in your code will refer to the literal string key, not the variable)

      – CertainPerformance
      Nov 15 '18 at 8:32













    • you can change it to { [key] : value } and it should compute the value.

      – Lloyd
      Nov 15 '18 at 8:33











    • Thanks for the catch.

      – Bhojendra Rauniyar
      Nov 15 '18 at 8:33











    • @CertainPerformance Sorry, what do you mean? Is there a reason for Object.assign over plain bracket notation?

      – Bhojendra Rauniyar
      Nov 15 '18 at 8:34











    • If you don't need to merge objects, or create a new object or something like that, Object.assign seems really weird to use over just data[key] = value IMO

      – CertainPerformance
      Nov 15 '18 at 8:36



















    0














    What you're trying to do is to implement a HashMap or a Dictionary.



    I'd just do it like so:



    function setData(key, value) 
    {
    if (data[key])
    data[key].push(value)
    else
    data[key] = [ value ]
    }





    share|improve this answer
























    • you are talking about Hashmap/Dictionary (a.k.a. "Object" in JS), but created an Array in your code

      – Aprillion
      Nov 17 '18 at 11:00



















    -1














    Check this solution






    const data = {}
    var fun = (key, value)=>{

    data[key] = value;
    }
    fun('kk',123);
    console.log(data);








    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%2f53315142%2fupdate-object-property-without-rewriting%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      The solution is:



      setData(key, value) {
      this.data[key] = { ...this.data[key], ...value };
      }





      share|improve this answer
























      • spread operator? why not ... ?

        – Tornike Shavishvili
        Nov 15 '18 at 15:29











      • @TornikeShavishvili what is the difference between the spread operator and '...'?

        – Bogdan Tushevskyi
        Nov 16 '18 at 8:42











      • In your case three dots are spread operator. In javascript three dots '...' have basically two meanings, 1. If used in function declaration it defines "rest prameters", please see following doxumentations: Functions Rest Parameters 2. In other cases it is interpreted as spread operator. Please see Expressions - Spread syntax

        – Tornike Shavishvili
        Nov 16 '18 at 8:52













      • @TornikeShavishvili yes, I know this, I don't understand your first question

        – Bogdan Tushevskyi
        Nov 16 '18 at 8:54











      • I think my comment confused you. I meant that spread operator is good to use. I have to be more clear next time.

        – Tornike Shavishvili
        Nov 16 '18 at 8:55
















      1














      The solution is:



      setData(key, value) {
      this.data[key] = { ...this.data[key], ...value };
      }





      share|improve this answer
























      • spread operator? why not ... ?

        – Tornike Shavishvili
        Nov 15 '18 at 15:29











      • @TornikeShavishvili what is the difference between the spread operator and '...'?

        – Bogdan Tushevskyi
        Nov 16 '18 at 8:42











      • In your case three dots are spread operator. In javascript three dots '...' have basically two meanings, 1. If used in function declaration it defines "rest prameters", please see following doxumentations: Functions Rest Parameters 2. In other cases it is interpreted as spread operator. Please see Expressions - Spread syntax

        – Tornike Shavishvili
        Nov 16 '18 at 8:52













      • @TornikeShavishvili yes, I know this, I don't understand your first question

        – Bogdan Tushevskyi
        Nov 16 '18 at 8:54











      • I think my comment confused you. I meant that spread operator is good to use. I have to be more clear next time.

        – Tornike Shavishvili
        Nov 16 '18 at 8:55














      1












      1








      1







      The solution is:



      setData(key, value) {
      this.data[key] = { ...this.data[key], ...value };
      }





      share|improve this answer













      The solution is:



      setData(key, value) {
      this.data[key] = { ...this.data[key], ...value };
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 15 '18 at 14:44









      Bogdan TushevskyiBogdan Tushevskyi

      14814




      14814













      • spread operator? why not ... ?

        – Tornike Shavishvili
        Nov 15 '18 at 15:29











      • @TornikeShavishvili what is the difference between the spread operator and '...'?

        – Bogdan Tushevskyi
        Nov 16 '18 at 8:42











      • In your case three dots are spread operator. In javascript three dots '...' have basically two meanings, 1. If used in function declaration it defines "rest prameters", please see following doxumentations: Functions Rest Parameters 2. In other cases it is interpreted as spread operator. Please see Expressions - Spread syntax

        – Tornike Shavishvili
        Nov 16 '18 at 8:52













      • @TornikeShavishvili yes, I know this, I don't understand your first question

        – Bogdan Tushevskyi
        Nov 16 '18 at 8:54











      • I think my comment confused you. I meant that spread operator is good to use. I have to be more clear next time.

        – Tornike Shavishvili
        Nov 16 '18 at 8:55



















      • spread operator? why not ... ?

        – Tornike Shavishvili
        Nov 15 '18 at 15:29











      • @TornikeShavishvili what is the difference between the spread operator and '...'?

        – Bogdan Tushevskyi
        Nov 16 '18 at 8:42











      • In your case three dots are spread operator. In javascript three dots '...' have basically two meanings, 1. If used in function declaration it defines "rest prameters", please see following doxumentations: Functions Rest Parameters 2. In other cases it is interpreted as spread operator. Please see Expressions - Spread syntax

        – Tornike Shavishvili
        Nov 16 '18 at 8:52













      • @TornikeShavishvili yes, I know this, I don't understand your first question

        – Bogdan Tushevskyi
        Nov 16 '18 at 8:54











      • I think my comment confused you. I meant that spread operator is good to use. I have to be more clear next time.

        – Tornike Shavishvili
        Nov 16 '18 at 8:55

















      spread operator? why not ... ?

      – Tornike Shavishvili
      Nov 15 '18 at 15:29





      spread operator? why not ... ?

      – Tornike Shavishvili
      Nov 15 '18 at 15:29













      @TornikeShavishvili what is the difference between the spread operator and '...'?

      – Bogdan Tushevskyi
      Nov 16 '18 at 8:42





      @TornikeShavishvili what is the difference between the spread operator and '...'?

      – Bogdan Tushevskyi
      Nov 16 '18 at 8:42













      In your case three dots are spread operator. In javascript three dots '...' have basically two meanings, 1. If used in function declaration it defines "rest prameters", please see following doxumentations: Functions Rest Parameters 2. In other cases it is interpreted as spread operator. Please see Expressions - Spread syntax

      – Tornike Shavishvili
      Nov 16 '18 at 8:52







      In your case three dots are spread operator. In javascript three dots '...' have basically two meanings, 1. If used in function declaration it defines "rest prameters", please see following doxumentations: Functions Rest Parameters 2. In other cases it is interpreted as spread operator. Please see Expressions - Spread syntax

      – Tornike Shavishvili
      Nov 16 '18 at 8:52















      @TornikeShavishvili yes, I know this, I don't understand your first question

      – Bogdan Tushevskyi
      Nov 16 '18 at 8:54





      @TornikeShavishvili yes, I know this, I don't understand your first question

      – Bogdan Tushevskyi
      Nov 16 '18 at 8:54













      I think my comment confused you. I meant that spread operator is good to use. I have to be more clear next time.

      – Tornike Shavishvili
      Nov 16 '18 at 8:55





      I think my comment confused you. I meant that spread operator is good to use. I have to be more clear next time.

      – Tornike Shavishvili
      Nov 16 '18 at 8:55













      0














      You have mentioned that you want to add as many values as youi call setData. That sounds like a job for array.



      You could use follwoing code:



      function setData(targetObj, propName, valueToAdd){

      if(typeof targetObj[propName] === 'undefined'){
      targetObj[propName] = ;
      }
      if(Array.isArray(targetObj[propName])){
      targetObj[propName].push(valueToAdd);
      }else{
      console.log('property '+propName +' already exists and is not an array');
      }
      }





      share|improve this answer




























        0














        You have mentioned that you want to add as many values as youi call setData. That sounds like a job for array.



        You could use follwoing code:



        function setData(targetObj, propName, valueToAdd){

        if(typeof targetObj[propName] === 'undefined'){
        targetObj[propName] = ;
        }
        if(Array.isArray(targetObj[propName])){
        targetObj[propName].push(valueToAdd);
        }else{
        console.log('property '+propName +' already exists and is not an array');
        }
        }





        share|improve this answer


























          0












          0








          0







          You have mentioned that you want to add as many values as youi call setData. That sounds like a job for array.



          You could use follwoing code:



          function setData(targetObj, propName, valueToAdd){

          if(typeof targetObj[propName] === 'undefined'){
          targetObj[propName] = ;
          }
          if(Array.isArray(targetObj[propName])){
          targetObj[propName].push(valueToAdd);
          }else{
          console.log('property '+propName +' already exists and is not an array');
          }
          }





          share|improve this answer













          You have mentioned that you want to add as many values as youi call setData. That sounds like a job for array.



          You could use follwoing code:



          function setData(targetObj, propName, valueToAdd){

          if(typeof targetObj[propName] === 'undefined'){
          targetObj[propName] = ;
          }
          if(Array.isArray(targetObj[propName])){
          targetObj[propName].push(valueToAdd);
          }else{
          console.log('property '+propName +' already exists and is not an array');
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 8:37









          Tornike ShavishviliTornike Shavishvili

          61021122




          61021122























              0














              Use like this:



              const data = {}

              setData(key, value) {
              Object.assign(data, {[key]: value});
              }


              BTW, You can simply set the data like:



              setData(key, value) {
              data[key] = value
              }





              share|improve this answer





















              • 1





                Is there a reason for Object.assign over plain bracket notation? (also, key in your code will refer to the literal string key, not the variable)

                – CertainPerformance
                Nov 15 '18 at 8:32













              • you can change it to { [key] : value } and it should compute the value.

                – Lloyd
                Nov 15 '18 at 8:33











              • Thanks for the catch.

                – Bhojendra Rauniyar
                Nov 15 '18 at 8:33











              • @CertainPerformance Sorry, what do you mean? Is there a reason for Object.assign over plain bracket notation?

                – Bhojendra Rauniyar
                Nov 15 '18 at 8:34











              • If you don't need to merge objects, or create a new object or something like that, Object.assign seems really weird to use over just data[key] = value IMO

                – CertainPerformance
                Nov 15 '18 at 8:36
















              0














              Use like this:



              const data = {}

              setData(key, value) {
              Object.assign(data, {[key]: value});
              }


              BTW, You can simply set the data like:



              setData(key, value) {
              data[key] = value
              }





              share|improve this answer





















              • 1





                Is there a reason for Object.assign over plain bracket notation? (also, key in your code will refer to the literal string key, not the variable)

                – CertainPerformance
                Nov 15 '18 at 8:32













              • you can change it to { [key] : value } and it should compute the value.

                – Lloyd
                Nov 15 '18 at 8:33











              • Thanks for the catch.

                – Bhojendra Rauniyar
                Nov 15 '18 at 8:33











              • @CertainPerformance Sorry, what do you mean? Is there a reason for Object.assign over plain bracket notation?

                – Bhojendra Rauniyar
                Nov 15 '18 at 8:34











              • If you don't need to merge objects, or create a new object or something like that, Object.assign seems really weird to use over just data[key] = value IMO

                – CertainPerformance
                Nov 15 '18 at 8:36














              0












              0








              0







              Use like this:



              const data = {}

              setData(key, value) {
              Object.assign(data, {[key]: value});
              }


              BTW, You can simply set the data like:



              setData(key, value) {
              data[key] = value
              }





              share|improve this answer















              Use like this:



              const data = {}

              setData(key, value) {
              Object.assign(data, {[key]: value});
              }


              BTW, You can simply set the data like:



              setData(key, value) {
              data[key] = value
              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 15 '18 at 8:38

























              answered Nov 15 '18 at 8:31









              Bhojendra RauniyarBhojendra Rauniyar

              51.7k2079128




              51.7k2079128








              • 1





                Is there a reason for Object.assign over plain bracket notation? (also, key in your code will refer to the literal string key, not the variable)

                – CertainPerformance
                Nov 15 '18 at 8:32













              • you can change it to { [key] : value } and it should compute the value.

                – Lloyd
                Nov 15 '18 at 8:33











              • Thanks for the catch.

                – Bhojendra Rauniyar
                Nov 15 '18 at 8:33











              • @CertainPerformance Sorry, what do you mean? Is there a reason for Object.assign over plain bracket notation?

                – Bhojendra Rauniyar
                Nov 15 '18 at 8:34











              • If you don't need to merge objects, or create a new object or something like that, Object.assign seems really weird to use over just data[key] = value IMO

                – CertainPerformance
                Nov 15 '18 at 8:36














              • 1





                Is there a reason for Object.assign over plain bracket notation? (also, key in your code will refer to the literal string key, not the variable)

                – CertainPerformance
                Nov 15 '18 at 8:32













              • you can change it to { [key] : value } and it should compute the value.

                – Lloyd
                Nov 15 '18 at 8:33











              • Thanks for the catch.

                – Bhojendra Rauniyar
                Nov 15 '18 at 8:33











              • @CertainPerformance Sorry, what do you mean? Is there a reason for Object.assign over plain bracket notation?

                – Bhojendra Rauniyar
                Nov 15 '18 at 8:34











              • If you don't need to merge objects, or create a new object or something like that, Object.assign seems really weird to use over just data[key] = value IMO

                – CertainPerformance
                Nov 15 '18 at 8:36








              1




              1





              Is there a reason for Object.assign over plain bracket notation? (also, key in your code will refer to the literal string key, not the variable)

              – CertainPerformance
              Nov 15 '18 at 8:32







              Is there a reason for Object.assign over plain bracket notation? (also, key in your code will refer to the literal string key, not the variable)

              – CertainPerformance
              Nov 15 '18 at 8:32















              you can change it to { [key] : value } and it should compute the value.

              – Lloyd
              Nov 15 '18 at 8:33





              you can change it to { [key] : value } and it should compute the value.

              – Lloyd
              Nov 15 '18 at 8:33













              Thanks for the catch.

              – Bhojendra Rauniyar
              Nov 15 '18 at 8:33





              Thanks for the catch.

              – Bhojendra Rauniyar
              Nov 15 '18 at 8:33













              @CertainPerformance Sorry, what do you mean? Is there a reason for Object.assign over plain bracket notation?

              – Bhojendra Rauniyar
              Nov 15 '18 at 8:34





              @CertainPerformance Sorry, what do you mean? Is there a reason for Object.assign over plain bracket notation?

              – Bhojendra Rauniyar
              Nov 15 '18 at 8:34













              If you don't need to merge objects, or create a new object or something like that, Object.assign seems really weird to use over just data[key] = value IMO

              – CertainPerformance
              Nov 15 '18 at 8:36





              If you don't need to merge objects, or create a new object or something like that, Object.assign seems really weird to use over just data[key] = value IMO

              – CertainPerformance
              Nov 15 '18 at 8:36











              0














              What you're trying to do is to implement a HashMap or a Dictionary.



              I'd just do it like so:



              function setData(key, value) 
              {
              if (data[key])
              data[key].push(value)
              else
              data[key] = [ value ]
              }





              share|improve this answer
























              • you are talking about Hashmap/Dictionary (a.k.a. "Object" in JS), but created an Array in your code

                – Aprillion
                Nov 17 '18 at 11:00
















              0














              What you're trying to do is to implement a HashMap or a Dictionary.



              I'd just do it like so:



              function setData(key, value) 
              {
              if (data[key])
              data[key].push(value)
              else
              data[key] = [ value ]
              }





              share|improve this answer
























              • you are talking about Hashmap/Dictionary (a.k.a. "Object" in JS), but created an Array in your code

                – Aprillion
                Nov 17 '18 at 11:00














              0












              0








              0







              What you're trying to do is to implement a HashMap or a Dictionary.



              I'd just do it like so:



              function setData(key, value) 
              {
              if (data[key])
              data[key].push(value)
              else
              data[key] = [ value ]
              }





              share|improve this answer













              What you're trying to do is to implement a HashMap or a Dictionary.



              I'd just do it like so:



              function setData(key, value) 
              {
              if (data[key])
              data[key].push(value)
              else
              data[key] = [ value ]
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 15 '18 at 8:45









              AlexLAlexL

              813




              813













              • you are talking about Hashmap/Dictionary (a.k.a. "Object" in JS), but created an Array in your code

                – Aprillion
                Nov 17 '18 at 11:00



















              • you are talking about Hashmap/Dictionary (a.k.a. "Object" in JS), but created an Array in your code

                – Aprillion
                Nov 17 '18 at 11:00

















              you are talking about Hashmap/Dictionary (a.k.a. "Object" in JS), but created an Array in your code

              – Aprillion
              Nov 17 '18 at 11:00





              you are talking about Hashmap/Dictionary (a.k.a. "Object" in JS), but created an Array in your code

              – Aprillion
              Nov 17 '18 at 11:00











              -1














              Check this solution






              const data = {}
              var fun = (key, value)=>{

              data[key] = value;
              }
              fun('kk',123);
              console.log(data);








              share|improve this answer




























                -1














                Check this solution






                const data = {}
                var fun = (key, value)=>{

                data[key] = value;
                }
                fun('kk',123);
                console.log(data);








                share|improve this answer


























                  -1












                  -1








                  -1







                  Check this solution






                  const data = {}
                  var fun = (key, value)=>{

                  data[key] = value;
                  }
                  fun('kk',123);
                  console.log(data);








                  share|improve this answer













                  Check this solution






                  const data = {}
                  var fun = (key, value)=>{

                  data[key] = value;
                  }
                  fun('kk',123);
                  console.log(data);








                  const data = {}
                  var fun = (key, value)=>{

                  data[key] = value;
                  }
                  fun('kk',123);
                  console.log(data);





                  const data = {}
                  var fun = (key, value)=>{

                  data[key] = value;
                  }
                  fun('kk',123);
                  console.log(data);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 8:49









                  GAURAVGAURAV

                  11211




                  11211






























                      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%2f53315142%2fupdate-object-property-without-rewriting%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

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python