Skip to content Skip to sidebar Skip to footer

How To Concatenate (variable + Object Key Names) To Get The Object Values In Dot Notation

Assuming I've a JSON object like this: var myObj = { 'question1': { 'option1': 'foo', 'option2': 'bar', 'option3': 'baz' }, 'question2': {

Solution 1:

Simply do

myObj['question'+i]

This is because the dot operator would not accept string with it as per javascript. So you will have to use square brackets instead which is often used to access properties of an object dynamically.

Post a Comment for "How To Concatenate (variable + Object Key Names) To Get The Object Values In Dot Notation"