Using the jQuery find or inArray method to find an element in an array

I was wondering if anyone knows how to use the jQuery search method or the inArray method to search for an element in an array. I can not find anything in the documents.

eg:

var items = [{id:1, name:'bob'}, {id:2, name:'joe'}, {id:3, name:'ben'}]; var found = $(items).find("[name='ben']"); 

or

 var items = [{id:1, name:'bob'}, {id:2, name:'joe'}, {id:3, name:'ben'}]; var found = $.inArray("[name='ben']", items); 
+7
jquery
source share
2 answers

I think you are looking for the RichArray plugin and more specifically

 $.RichArray.filter() 

You can take it on RichArray

+3
source share

The jQuery search method works with the DOM. If you try to find an array, you are likely to hit this code:

 // check to make sure context is a DOM element or a document if ( context && context.nodeType != 1 && context.nodeType != 9) return [ ]; 

This can be found in the jQuery source for the find method. It always returns an empty array unless you use a DOM element or document as your context.

0
source share

All Articles