1

I have a situation where I have two arrays of objects. I want select same objects from both arrays and show it. For instance I have following UL (You can see it at jsFiddle):

<ul>
<li id="one" class="color pro">one</li>
<li id="two" class="color pro">two</li>
<li id="three" class="color">three</li>
<li id="four" class="color">four</li>
</ul>

I am using following script to show same objects.

var activeElementsColors = $("ul li.color");
var activeElementsPro = $("ul li.pro");                             

var activeElements = activeElementsPro.filter(function(el) {  
return $.inArray(el, activeElementsColors) > -1;
});

activeElements.show();

I know I can use $("li.color.pro").show() but above one is just an example. real code is complex than this.

1
  • The $("ul li.color") will already give you both sets combined. Commented Dec 11, 2011 at 17:35

4 Answers 4

2
var activeElementsColors = $("ul li.color");
var activeElementsPro = $("ul li.pro");                             

var activeElements = activeElementsPro.filter(function(el,e) {  

    return $.inArray(e, activeElementsColors) > -1;
});
console.log(activeElements);
activeElements.show();

http://jsfiddle.net/StPew/8/

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

3 Comments

I am wondering which one is more efficient. Your's or pimvdb's? btw both works fine for me ;)
also if I have more groups like for example: activeElementsColors, activeElementsPro, activeElementsType, activeElementsSize etc?
I don't have the jQuery sources (and I doubt I'd be able to read them), but I suspect pimvdb's is more efficient really, because it skips a dereferencing that you usually woudn't program naively with an overload in an API. Profile it dpoing it a thousand times, get back with some numbers?
2

The first argument in the function passed to .filter is the index of the element in the jQuery object, not the element itself. Use this instead, which does refer to the element: http://jsfiddle.net/StPew/7/.

var activeElements = activeElementsPro.filter(function() {
    return $.inArray(this, activeElementsColors) > -1;
});

From the docs:

function(index)

A function used as a test for each element in the set. this is the current DOM element.

2 Comments

I am wondering which one is more efficient. Your's or 3nigma's? btw both works fine for me ;)
i'll leave the explanation to @pimvdb :)
1

The parameter to the filter callback is the index of the current element. Use this instead.

return $.inArray(this, activeElementsColors) > -1;

That said what you doing seems seriously wrong. Assuming what you actually want is intersection of .pro and .color selectors, then you can simply do

$(".pro:.color").show();

fiddle

Comments

1

.filter() takes a jQuery object as a parameter and returns a new object that consists of the elements in the first object minus the ones that aren't in the second jQuery object, so if you already have the jQuery objects, you can use them like this to show the objects that are in both:

var activeElementsColors = $("ul li.color");
var activeElementsPro = $("ul li.pro"); 
activeElementsPro.filter(activeElementsColors).show();

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.