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++;
}
1

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Yep that did it, I knew it would be something blindly obvious, now I feel bad for asking. Thanks for your help!
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.