0

For example, I have JavaScript array like this:

var data = [
{text:'Business', aid:'6'}, 
{text:'Careers', aid:'29'}, 
{text:'Credits', aid:'28'}, 
{text:'Insurance', aid:'30'}
];

Now, in URl is passed ID and I need to grab 'text' from this array based on that ID like:

$('#category').val(data, function(item) {return item.aid['6'].text;} ); // = Business

So I want to populate form field #category with text from array based on passed URL ID = aid?

Thanks in advance

1 Answer 1

1

Use the filter[MDN] method to get the right object:

var aid = 28,
    match = data.filter(function(elem) { return elem.aid == aid; });

if (match.length) {
    $('#category').val(match[0].text);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @FishBasketGordo Just discovered that IE8 makes error like: "Message: Object doesn't support this property or method". Could You advice me what to do? Thanks
Found solution, just changed to this: match = $.grep(data,function(elem){ return elem.aid == aid; });

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.