How do I get an item from a JavaScript object:
var items = [
{
ITEM:1,
AMOUNT:10
},
{
ITEM:2,
AMOUNT:20
}
];
I want to be able to do something like this:
$(items).filter(ITEM == 1).AMOUNT;
… which would return 10.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the Array
filtermethod, which returns a new array containing all matching elements. (there could be more than one matching item)Note that IE6 (I’m not sure about newer versions) doesn’t support the
filtermethod. You could always define it yourself if it doesn’t exist: