Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
150
rated 0 times [  154] [ 4]  / answers: 1 / hits: 20648  / 10 Years ago, sat, march 1, 2014, 12:00:00

Say that I have an object with key/value pair as the following:



var someVar = {
color: white,
font_size: 30px,
font_weight: normal
...some more variables and functions
};


Is there a way to do a multiple assignment to those keys instead of having to do something like this:



someVar.color = blue;
someVar.font_size = 30px;
...

More From » object

 Answers
48

You could loop through another object:



var thingsToAdd = {
color: blue,
font_size: 30px
};
for (var x in thingsToAdd) someVar[x] = thingsToAdd[x];


Or, you could use with (WARNING: this is ALMOST CERTAINLY a bad idea! See the link. I am only posting this for educational purposes; you should almost never use with in production code!):



with (someVar) {
color = blue;
font_size = 30px;
}

[#72227] Thursday, February 27, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
antoinette

Total Points: 206
Total Questions: 99
Total Answers: 95

Location: Guam
Member since Tue, Nov 29, 2022
2 Years ago
;