Searching in Array using jQuery

Go To StackoverFlow.com

1

I have the following jQuery and I'm confused as to why it's returning -1 and not 0 ? Is anyone able to explain:

var one = $('.test');
var two = $('.test2');

var myArray = [];
myArray.push(one);

if($.inArray($('.test'), myArray) < 0) {
   myArray.push(two);         
}

console.log($.inArray($('.test'), myArray)); //return -1

I setup http://jsfiddle.net/ecrMw/1/ and I'm trying to return 0 for $('.test') ? That is, find it in the array ?

2012-04-04 18:27
by Andy
Wouldn't you want to test console.log($.inArray(one, myArray)); which does return 0 - j08691 2012-04-04 18:32


2

Because the jQuery object is actually a collection not a DOM element. What you're looking for is the facsimile of:

var one = $('.test')[0];
var two = $('.test2')[0];

// on with your code

Then you'd test again against the element:

if ($.inArray($('.test')[0], myArray)){
}

otherwise (notated bluntly) you're adding arrays to an array:

var one = $('.test'); // actually a [$] array
var two = $('.test2'); // actually another [$] array

var myArray = []; // empty array
myArray.push(one); // one = [[$]];

Then comparing if an array is within another array becomes a more complex issue.

2012-04-04 18:32
by Brad Christie
thanks brad. I'm trying to add a bunch of elements to an array and then search across all elements added to the array ? I did try [0] but that doesn't search across everything in the array - Andy 2012-04-04 18:34
@Andy: So why aren't you using .find(), .has() or .is() - Brad Christie 2012-04-04 18:36
Unclear how I could use those to search across values added to myArray - Andy 2012-04-04 18:37
@Andy: I guess the question is this: What are you trying to accomplish? Given that jQuery objects and how you're approaching it isn't working, what's the original problem and I'll see if I can't find a better solution. ;- - Brad Christie 2012-04-04 18:38
Sure :) I'm adding multiple [$] to myArray which can contain multiple classes i.e. [$('.test'), $('.test12')]. Then all I'm basically trying to do - if the element is already in the array, ignore otherwise add - Andy 2012-04-04 18:42
@Andy: So step back a second. Why not just use: var classes = []; classes.push('test1'); if (!$.inArray('test2',classes)){ classes.push('test2'); } then you can turn it in to a query (either as an "AND" ($('.'+classes.join('.'))) or "OR" ($('.'+classes.join(',.'))) type match - Brad Christie 2012-04-04 18:49


0

var one = $('.test');

console.log($('.test') == one); //Outputs false

Javascript does not know how to compare the two objects therefore their comparison returns false. I am guessing that its looking at the memory address to determine their equality.

Since js cannot determine their equality, I'm guessing the jquery library does not handle objects in the inArray() method.

2012-04-04 18:32
by Kevin Bowersox


0

Check this fiddle ,I have re-wrote it

e.g

 var found = $('.test,.test2').map(function(){return $(this).get(0)})

  var foundAry=$.makeArray(found);

  console.log($.inArray($('.test').get(0), foundAry));

The point is that you need to search for DOM object in array not for JQUERY object in array.

hope this will help you.

2012-04-04 18:46
by sakhunzai


0

Why use an array? You could use a jQuery collection.

var $one = $('.test'),
    $two = $('.test2'),
    $myCollection = $();

$.merge( $myCollection, $one );

if( $myCollection.index( $one ) < 0) {
   $.merge( $myCollection, $two );  
}

console.log( $myCollection.index( $one )  ); //returns 0
2012-04-04 19:04
by Michael Sparks
Ads