8

Given an item and an array, I would like to know if item exist in array.

item is a jQuery object, e.g. $(".c"). You can assume that item.length == 1.

array is an array of jQuery objects, e.g. [$(".a"), $(".b")]. Each item in this array may represent 0, 1, or more objects.

Here is how I thought to implement this: (live demo here)

function inArray(item, arr) {
    for (var i = 0; i < arr.length; i++) {
        var items = $.makeArray(arr[i]);

        for (var k = 0; k < items.length; k++) {
            if (items[k] == item[0]) {
                return true;
            }
        }
    }

    return false;
}

Can you find a more elegant implementation?


Example:

HTML:

<div class="a">Hello</div>
<div class="a">Stack</div>
<div class="a">Overflow</div>

<div class="b">Have</div>
<div class="b">a</div>
<div class="b">nice</div>
<div class="b">day!</div>

<div class="c">Bye bye</div>

JS:

console.log(inArray($(".a").eq(2), [$(".a"), $(".b")])); // true
console.log(inArray($(".b").eq(3), [$(".a"), $(".b")])); // true
console.log(inArray($(".c"), [$(".a"), $(".b")]));       // false
console.log(inArray($(".a").eq(2), [$(".b")]));          // false
console.log(inArray($(".a").eq(2), []));                 // false
console.log(inArray($(".c"), [$("div")]));               // true
3
  • 2
    Does it have to be an array? Why don't you use a jQuery object and .index()? Commented Jan 7, 2012 at 10:37
  • @Felix: I guess you mean to use $(".a, .b"). Sounds reasonable! Commented Jan 7, 2012 at 10:48
  • 1
    Or you can use add() to build up the jQuery object. Commented Jan 7, 2012 at 10:57

5 Answers 5

10

According to Felix's suggestion:

[$(selector1), $(selector2), ... ] can be simplified to

$(selector1, selector2, ...)

or

$(selector1).add(selector2)...

and then it can be implemented as:

function inArray(item, arr) {
  return (arr.index(item) != -1);
}

Live demo here

Sign up to request clarification or add additional context in comments.

1 Comment

Good solution! I don't think this really needs a function wrapper, though - while the OP was in its own function, just doing the one line inside the function here is more than enough.
8

what about

if(jQuery.inArray(some, array) === -1)
{
//process data if "some" is not in array
}
else
{
//process if "some" is in array
}

read here : http://api.jquery.com/jQuery.inArray/

2 Comments

Although upvoted many times, this is NOT working for an array that contains jQuery objects (which is what the OP asked for). See Misha's own answer for a correct solution, using jQuery's .index() method.
This solution does not work for array of jquery elements
0
if($.inArray("valueYouWantToFind", nameOfTheArray) == true) {
  Your code;
 }

 Eg.,
 var userChoice = ["yes"];
 if($.inArray('yes',userChoice) == true) {
                    alert("found");
                }

Comments

0
console.log(!!~$.inArray("a", ["a", "b", "c"]));

2 Comments

Could you elaborate on this a little? "Try this" style answers don't explain much and encourages the same questions to get asked over and over.
Agreeing with the comment above: elaborating on why this is an answer would help everyone, especially where you're using two of JavaScript's "tricks" in one line.
-1
data = [
  {val:'xxx',txt:'yyy'},
  {val:'yyy',txt:'aaa'},
  {val:'bbb',txt:'ccc'}
];

            var dummyArray = [];
            var distinctValueArray = [];

            $.each(data, function (index, firstobject) {
              //push first element of object in both dummy array and distinct array.

               if (index == 0) {
                    distinctValueArray.push(firstobject);
                    dummyArray.push(firstobject.txt);
                }
                else {

                    //started from 2nd index.

                   if ($.inArray(firstobject.txt, dummyArray) == -1) {
                        distinctValueArray.push(firstobject);
                    }
                    dummyArray.push(firstobject.txt);
                }
            });
            dummyArray.length=0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.