Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leveraging JQuery find or inArray method to find an item in an array

Tags:

jquery

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);
like image 265
bendewey Avatar asked Jan 05 '09 18:01

bendewey


People also ask

How do you check if an element exists in an array in jQuery?

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 .

What is inArray in jQuery?

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.

How do you check if a string is present in an array in jQuery?

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.


1 Answers

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

$.RichArray.filter()

You can grab it at RichArray

like image 179
Sarat Avatar answered Sep 27 '22 21:09

Sarat