0

I am facing problem with dynamic selector. Here is my code

var data = {
           'one': {'value': '50,60,70,80'},
           'two': {'value': '10,20,30,40'}
           }
var eachValue = data.one.value.split(',');

That code is working correcting but i need dynamic selector for one two so if i code like

var select = 'one';
var eachValue = data.select.value.split(',');

its not working. its showing TypeError: data.select is undefined error. So how can i use dynamic selector within this. Thanks

1 Answer 1

1

You can use bracket notation if you want to use variable as property name so data[select]

var data = {
  'one': {'value': '50,60,70,80'},
  'two': {'value': '10,20,30,40'}
}

var select = 'two';
var eachValue = data[select].value.split(',');
console.log(eachValue)

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.