0

I want to create the json element dynamically..something like this:

for exam. if

var branch_name = "abc-cde";

Instead of "abc-cde" below I want the variable branch_name, so that I can replace it with other elements. please note the hyphen in abc-cde.

itemsarray["abc-cde_workunit"] = dataResponse.branch_descriptor["abc-cde"].workunit_id;

The above code is in angularjs. Please let me know if anything is not clear.

2 Answers 2

1

It's actually simpler than you might think.

itemsarray["abc-cde_workunit"] = dataResponse.branch_descriptor[branch_name].workunit_id;
Sign up to request clarification or add additional context in comments.

2 Comments

in left hand side variable, abc-cde also I need as variable. something like this itemsarray["<branch_name>_workunit"]
In that case just replace itemsarray["abc-cde_workunit"] with itemsarray[branch_name_workunit] and make sure you set branch_name_workunit variable to a property of itemsarray.
0

does

itemsarray[branch_name + "_workunit"] = dataResponse.branch_descriptor[branch_name].workunit_id;

not do what you're after?

2 Comments

saved my day...actually night.. thanks a lot @Stuart :)
my pleasure - pleased to be of some help! :)

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.