0

I've got a little problem with a JavaScript function (I'm pretty new to JavaScript in general) to which I pass a String(cat) and a JSON string (args) I guess you know what I like to achieve: if I pass foo as 'cat' I'd like to parse args.foo:

function addchardata(cat, args){
    var data= JSON.parse(args+'.'+cat);    ///this does not work...
}  
coder771
  • 368
  • 5
  • 17
Mike Balthasar
  • 173
  • 1
  • 7
  • 1
    You do realize that creates invalid JSON, right? – Sterling Archer Dec 09 '15 at 16:18
  • A small example of what you would like to achieve would be helpful. It's not that easy to guess what you want as output – Endless Dec 09 '15 at 16:22
  • 1
    "I guess you know what i like to achieve" Nope, pretend I'm not clairvoyant and explain explicitly, please. – FuriousFolder Dec 09 '15 at 16:22
  • What is your question? – David Pine Dec 09 '15 at 16:23
  • Ah ok, sorry: What i meant was: "JSON.parse(args.one)" works the way I want it (delivering the data array for a highcharts chart, one is a jsf Callback-Parameter), but what can I do to get the same result by passing args and a String "one" to a function? – Mike Balthasar Dec 09 '15 at 16:40
  • @MikeBalthasar: The answers to the linked question show you how to do that. Short version: `args[cat]` will access the `one` property on `args` if `cat` contains the string `"one"`. E.g., `var cat = "one"; console.log(args[cat]);` and `console.log(args.one);` both access the same property. – T.J. Crowder Dec 09 '15 at 16:41

0 Answers0