Given I have an array of "purpose" objects:
//array of purpose objects:
var purposeObjects = [
{purpose: "daily"},
{purpose: "weekly"},
{purpose: "monthly"}
];
(for simplicity i am omitting other attributes)
Now I want to have a method that returns a specific one of the objects if a matching purpose name is found.
This is not working:
function findPurpose(purposeName){
return $.grep(purposeObjects, function(){
return this.purpose == purposeName;
});
};
findPurpose("daily");
but it actually returns an empty array:
[]
I am using JQuery 1.5.2. I have also tried with $.each() but with no luck.
Apparently, most JQuery methods are designed for usage with DOM elements (such as filter()
.
Any ideas on how to achieve this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…