I was wondering if anyone knows of a way to use the JQuery find method or inArray method to find an item in an array. I can't seem to find anything in the docs.
for example:
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);
inArray() This jQuery array method search the the item within the array. If element exists in the jQuery array it returns the index position of the value and if the value doesn't exist then it will return -1 .
The jQuery inArray() method is used to find a specific value in the given array. If the value found, the method returns the index value, i.e., the position of the item. Otherwise, if the value is not present or not found, the inArray() method returns -1. This method does not affect the original array.
1) Using jQuery If you are someone strongly committed to using the jQuery library, you can use the . inArray( ) method. If the function finds the value, it returns the index position of the value and -1 if it doesn't.
I think what you are looking for is the RichArray plugin and more specifically
$.RichArray.filter()
You can grab it at RichArray
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With