Assuming I have a json variable inside a javascript file,
{
A : "a",
B : "b",
C : "c"
}
now, I have a javascript variable and append the value to the value of the json variable. Somethinhg like this
{
A : "a" + var1,
B : "b" + var2 ,
C : "c" + var3
}
is it possible??
Something like below:
var obj = { A : "a", B : "b", C : "c" };
obj.A += var1;
obj.B += var2;
obj.C += var3;
Use Object.keys
and forEach
:
var obj = {
A: "a",
B: "b",
C: "c"
},
vars = [var1, var2, var3];
Object.keys(obj).forEach(function (key, i) {
obj[key] += vars[i];
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.