2

I’m trying to filter data from JSON in JavaScript.

I define a variable a. I want the property of whatever value a is equal to (not item.a). So far I’ve been unable to find a way of doing it.

Everything else is working correctly because when I changed it to a specific entry (item.date for example) it works fine. I cannot figure out the correct syntax.

while(i< elements.length){
    var a=elements[i].id;
    if(elements[i].name == 'targetfeild'){
      $(elements[i]).val($.map(result,function(item){var test = elements[i].id;return item.a;}));
    }
    i++;
}
Aaron Kurtzhals
  • 2,036
  • 3
  • 17
  • 21
Spag-bol
  • 35
  • 4
  • exact duplicate of [JavaScript object: access variable property by name as string](http://stackoverflow.com/questions/4255472/javascript-object-access-variable-property-by-name-as-string) – Bergi Aug 06 '13 at 14:02

1 Answers1

2

Try item[a], javascript objects can also be accessed this way.

iConnor
  • 19,997
  • 14
  • 62
  • 97
MaxPRafferty
  • 4,819
  • 4
  • 32
  • 39
  • Yep that did it, I knew it would be something blindly obvious, now I feel bad for asking. Thanks for your help! – Spag-bol Aug 06 '13 at 13:30
  • @Spag-bol , No worries, in your defense JS's pseudo-associative-array pattern is fairly non obvious. If this answered your question please mark it answered using the checkmark to the left. – MaxPRafferty Aug 06 '13 at 13:40