Update multiple values of object in JavaScript [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
11
down vote

favorite
1













This question already has an answer here:



  • Extending an Object in Javascript

    14 answers



  • How to assign multiple values to a JavaScript object?

    3 answers



Given an object



var myObject = 
label: 'foo',
name: 'bar',
id: 12
,


If I wanted to change multiple values, I would do the following:



myObject.label = "bar";
myObject.name = "foo";


When updating large sets of data, it makes the code quite blocky. Is there a way to do this in a more concise manner?



Like:



myObject.(label: 'foo', name: 'bar');






share|improve this question














marked as duplicate by nhahtdh, cezar, Salman A javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Aug 18 at 20:31


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















    up vote
    11
    down vote

    favorite
    1













    This question already has an answer here:



    • Extending an Object in Javascript

      14 answers



    • How to assign multiple values to a JavaScript object?

      3 answers



    Given an object



    var myObject = 
    label: 'foo',
    name: 'bar',
    id: 12
    ,


    If I wanted to change multiple values, I would do the following:



    myObject.label = "bar";
    myObject.name = "foo";


    When updating large sets of data, it makes the code quite blocky. Is there a way to do this in a more concise manner?



    Like:



    myObject.(label: 'foo', name: 'bar');






    share|improve this question














    marked as duplicate by nhahtdh, cezar, Salman A javascript
    Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    Aug 18 at 20:31


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















      up vote
      11
      down vote

      favorite
      1









      up vote
      11
      down vote

      favorite
      1






      1






      This question already has an answer here:



      • Extending an Object in Javascript

        14 answers



      • How to assign multiple values to a JavaScript object?

        3 answers



      Given an object



      var myObject = 
      label: 'foo',
      name: 'bar',
      id: 12
      ,


      If I wanted to change multiple values, I would do the following:



      myObject.label = "bar";
      myObject.name = "foo";


      When updating large sets of data, it makes the code quite blocky. Is there a way to do this in a more concise manner?



      Like:



      myObject.(label: 'foo', name: 'bar');






      share|improve this question















      This question already has an answer here:



      • Extending an Object in Javascript

        14 answers



      • How to assign multiple values to a JavaScript object?

        3 answers



      Given an object



      var myObject = 
      label: 'foo',
      name: 'bar',
      id: 12
      ,


      If I wanted to change multiple values, I would do the following:



      myObject.label = "bar";
      myObject.name = "foo";


      When updating large sets of data, it makes the code quite blocky. Is there a way to do this in a more concise manner?



      Like:



      myObject.(label: 'foo', name: 'bar');




      This question already has an answer here:



      • Extending an Object in Javascript

        14 answers



      • How to assign multiple values to a JavaScript object?

        3 answers









      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 16 at 8:50









      Peter Mortensen

      12.9k1983111




      12.9k1983111










      asked Aug 16 at 0:01









      agreis1

      1794




      1794




      marked as duplicate by nhahtdh, cezar, Salman A javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Aug 18 at 20:31


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by nhahtdh, cezar, Salman A javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Aug 18 at 20:31


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          16
          down vote



          accepted










          Object.assign is nice for this:






          var myObject = 
          label: 'foo',
          name: 'bar',
          id: 12

          Object.assign(myObject, label: 'Test', name: 'Barbar')
          console.log(myObject)








          share|improve this answer



























            up vote
            6
            down vote













            In addition to Object.assign, you can also use the object spread operator:






            var myObject = 
            label: 'foo',
            name: 'bar',
            id: 12
            ;

            myObject = ...myObject, label: 'baz', name: 'qux';
            console.log(myObject);

            // Or, if your update is contained in its own object:

            var myUpdate =
            label: 'something',
            name: 'else'


            myObject = ...myObject, ...myUpdate
            console.log(myObject)








            share|improve this answer
















            • 2




              Unlike Object.assign, this doesn’t actually “update” the object, it makes a whole new one, so if myObject is aliased the original version will remain visible there unchanged. That may or may not be ok.
              – Michael Homer
              Aug 16 at 4:25

















            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            16
            down vote



            accepted










            Object.assign is nice for this:






            var myObject = 
            label: 'foo',
            name: 'bar',
            id: 12

            Object.assign(myObject, label: 'Test', name: 'Barbar')
            console.log(myObject)








            share|improve this answer
























              up vote
              16
              down vote



              accepted










              Object.assign is nice for this:






              var myObject = 
              label: 'foo',
              name: 'bar',
              id: 12

              Object.assign(myObject, label: 'Test', name: 'Barbar')
              console.log(myObject)








              share|improve this answer






















                up vote
                16
                down vote



                accepted







                up vote
                16
                down vote



                accepted






                Object.assign is nice for this:






                var myObject = 
                label: 'foo',
                name: 'bar',
                id: 12

                Object.assign(myObject, label: 'Test', name: 'Barbar')
                console.log(myObject)








                share|improve this answer












                Object.assign is nice for this:






                var myObject = 
                label: 'foo',
                name: 'bar',
                id: 12

                Object.assign(myObject, label: 'Test', name: 'Barbar')
                console.log(myObject)








                var myObject = 
                label: 'foo',
                name: 'bar',
                id: 12

                Object.assign(myObject, label: 'Test', name: 'Barbar')
                console.log(myObject)





                var myObject = 
                label: 'foo',
                name: 'bar',
                id: 12

                Object.assign(myObject, label: 'Test', name: 'Barbar')
                console.log(myObject)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 16 at 0:03









                Mark Meyer

                23.5k32445




                23.5k32445






















                    up vote
                    6
                    down vote













                    In addition to Object.assign, you can also use the object spread operator:






                    var myObject = 
                    label: 'foo',
                    name: 'bar',
                    id: 12
                    ;

                    myObject = ...myObject, label: 'baz', name: 'qux';
                    console.log(myObject);

                    // Or, if your update is contained in its own object:

                    var myUpdate =
                    label: 'something',
                    name: 'else'


                    myObject = ...myObject, ...myUpdate
                    console.log(myObject)








                    share|improve this answer
















                    • 2




                      Unlike Object.assign, this doesn’t actually “update” the object, it makes a whole new one, so if myObject is aliased the original version will remain visible there unchanged. That may or may not be ok.
                      – Michael Homer
                      Aug 16 at 4:25














                    up vote
                    6
                    down vote













                    In addition to Object.assign, you can also use the object spread operator:






                    var myObject = 
                    label: 'foo',
                    name: 'bar',
                    id: 12
                    ;

                    myObject = ...myObject, label: 'baz', name: 'qux';
                    console.log(myObject);

                    // Or, if your update is contained in its own object:

                    var myUpdate =
                    label: 'something',
                    name: 'else'


                    myObject = ...myObject, ...myUpdate
                    console.log(myObject)








                    share|improve this answer
















                    • 2




                      Unlike Object.assign, this doesn’t actually “update” the object, it makes a whole new one, so if myObject is aliased the original version will remain visible there unchanged. That may or may not be ok.
                      – Michael Homer
                      Aug 16 at 4:25












                    up vote
                    6
                    down vote










                    up vote
                    6
                    down vote









                    In addition to Object.assign, you can also use the object spread operator:






                    var myObject = 
                    label: 'foo',
                    name: 'bar',
                    id: 12
                    ;

                    myObject = ...myObject, label: 'baz', name: 'qux';
                    console.log(myObject);

                    // Or, if your update is contained in its own object:

                    var myUpdate =
                    label: 'something',
                    name: 'else'


                    myObject = ...myObject, ...myUpdate
                    console.log(myObject)








                    share|improve this answer












                    In addition to Object.assign, you can also use the object spread operator:






                    var myObject = 
                    label: 'foo',
                    name: 'bar',
                    id: 12
                    ;

                    myObject = ...myObject, label: 'baz', name: 'qux';
                    console.log(myObject);

                    // Or, if your update is contained in its own object:

                    var myUpdate =
                    label: 'something',
                    name: 'else'


                    myObject = ...myObject, ...myUpdate
                    console.log(myObject)








                    var myObject = 
                    label: 'foo',
                    name: 'bar',
                    id: 12
                    ;

                    myObject = ...myObject, label: 'baz', name: 'qux';
                    console.log(myObject);

                    // Or, if your update is contained in its own object:

                    var myUpdate =
                    label: 'something',
                    name: 'else'


                    myObject = ...myObject, ...myUpdate
                    console.log(myObject)





                    var myObject = 
                    label: 'foo',
                    name: 'bar',
                    id: 12
                    ;

                    myObject = ...myObject, label: 'baz', name: 'qux';
                    console.log(myObject);

                    // Or, if your update is contained in its own object:

                    var myUpdate =
                    label: 'something',
                    name: 'else'


                    myObject = ...myObject, ...myUpdate
                    console.log(myObject)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 16 at 0:22









                    CRice

                    9,7781228




                    9,7781228







                    • 2




                      Unlike Object.assign, this doesn’t actually “update” the object, it makes a whole new one, so if myObject is aliased the original version will remain visible there unchanged. That may or may not be ok.
                      – Michael Homer
                      Aug 16 at 4:25












                    • 2




                      Unlike Object.assign, this doesn’t actually “update” the object, it makes a whole new one, so if myObject is aliased the original version will remain visible there unchanged. That may or may not be ok.
                      – Michael Homer
                      Aug 16 at 4:25







                    2




                    2




                    Unlike Object.assign, this doesn’t actually “update” the object, it makes a whole new one, so if myObject is aliased the original version will remain visible there unchanged. That may or may not be ok.
                    – Michael Homer
                    Aug 16 at 4:25




                    Unlike Object.assign, this doesn’t actually “update” the object, it makes a whole new one, so if myObject is aliased the original version will remain visible there unchanged. That may or may not be ok.
                    – Michael Homer
                    Aug 16 at 4:25


                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    Confectionery