1

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??

2
  • I suggest you convert your json string back to an object, and then change the property, and convert it back to json string; otherwise finding a variable and changing right in middle of string could be dangerous Commented Mar 7, 2014 at 7:27
  • This is object literal, not JSON. Commented Mar 7, 2014 at 7:27

2 Answers 2

3

Something like below:

var obj = { A : "a", B : "b", C : "c" };
obj.A += var1;
obj.B += var2;
obj.C += var3;
Sign up to request clarification or add additional context in comments.

Comments

0

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];
});

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.