IqbalBary
IqbalBary

Reputation: 1126

Dynamic jQuery selector within object selector

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

Upvotes: 0

Views: 21

Answers (1)

Nenad Vracar
Nenad Vracar

Reputation: 122125

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)

Upvotes: 1

Related Questions