0

I call contents from a JSON object which contains values for different sides like side_a = 100, side_b = 200 etc.

Now I want to get the values for every selected side.. this is normally archieved like obj.side_a

var obj = $.parseJSON(data); // Inside this are values like side_a, side_b etc.

I call each value like

obj.side_a

Now I want to call this variables inside a loop (the loop returns the available sides - like a, b, c ..), because I just want to display the values of selected sides.

so the 'a' part should be added dynamically to the call..

like obj.side_+sideResultFromTheLoop to get a call like obj.side_a, obj.side_b

I tried it on different ways.. like:

obj.+'side_'+theLoopVariable

.'obj.side'+theLoopVariable

but none of it worked :(

0

1 Answer 1

1

Use a different type of syntax with brackets:

obj['side_' + theLoopVariable] = objectData;

Read more about property accessors.

Sign up to request clarification or add additional context in comments.

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.