Update multiple values of object in JavaScript [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
11
down vote
favorite
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');
javascript
marked as duplicate by nhahtdh, cezar, Salman A
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.
add a comment |Â
up vote
11
down vote
favorite
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');
javascript
marked as duplicate by nhahtdh, cezar, Salman A
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.
add a comment |Â
up vote
11
down vote
favorite
up vote
11
down vote
favorite
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');
javascript
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
javascript
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
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
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.
add a comment |Â
add a comment |Â
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)
add a comment |Â
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)
2
UnlikeObject.assign
, this doesn’t actually “update†the object, it makes a whole new one, so ifmyObject
is aliased the original version will remain visible there unchanged. That may or may not be ok.
– Michael Homer
Aug 16 at 4:25
add a comment |Â
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)
add a comment |Â
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)
add a comment |Â
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)
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)
answered Aug 16 at 0:03


Mark Meyer
23.5k32445
23.5k32445
add a comment |Â
add a comment |Â
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)
2
UnlikeObject.assign
, this doesn’t actually “update†the object, it makes a whole new one, so ifmyObject
is aliased the original version will remain visible there unchanged. That may or may not be ok.
– Michael Homer
Aug 16 at 4:25
add a comment |Â
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)
2
UnlikeObject.assign
, this doesn’t actually “update†the object, it makes a whole new one, so ifmyObject
is aliased the original version will remain visible there unchanged. That may or may not be ok.
– Michael Homer
Aug 16 at 4:25
add a comment |Â
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)
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)
answered Aug 16 at 0:22
CRice
9,7781228
9,7781228
2
UnlikeObject.assign
, this doesn’t actually “update†the object, it makes a whole new one, so ifmyObject
is aliased the original version will remain visible there unchanged. That may or may not be ok.
– Michael Homer
Aug 16 at 4:25
add a comment |Â
2
UnlikeObject.assign
, this doesn’t actually “update†the object, it makes a whole new one, so ifmyObject
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
add a comment |Â