0

I have a <SELECT multiple="multiple"> box with options.

I store the selected options in a JS variable - selectedFeatures.

I want to pass selectedFeatures array variable of options to jQuery.inArray() - but the function is designed to take and check for one value only.

I can show/hide( or filter ) the markers on my map if I actually specify an index of an element like so: selectedFeatures[0] inside inArray()

..but since I may select multiple options from a select box this method of filtering doesn't work.

How can I check if an array of items in found inside another array with JS/jQuery?

// store selected options as an array of string elements inside the variable
var selectedFeatures = $("#features").val();
// for every element inside 'markers.houses' array...                   
$(markers.houses).each(function(index, elem){
    // check if any of the values inside the 'selectedFeatures' array are found
    // also inside a sub-array 'features' inside every element of 'markers.houses'
    // array. if 'true' show that particular marker, otherwise hide the marker
    if(jQuery.inArray(selectedFeatures, elem.features) !== -1 || jQuery.inArray(selectedFeatures, elem.features) > -1){
        markers.houseMarkers[index].setVisible(true);
    }else{
        markers.houseMarkers[index].setVisible(false);
    }
});

Fig 1.1 - store selected options in an array variable and use it with jQuery.inArray()

2 Answers 2

1

What you are asking for can be solved with PHP's array_diff function. I know you're not using PHP, but that's why there is array_diff on PHPJS, a project dedicated to porting PHP functions into JavaScript.

Anyway, to solve this you basically want to call array_diff(selectedFeatures,elem.features).length == 0. (You may need to swap the arguments around, I'm not sure which array is which in your question)

array_diff returns the elements of the first array that are not present in the others. If all of the elements in the first array are also in the second array (ie. array1 is a subset of array2), then the returned array will be empty, hence its length is zero.

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

6 Comments

Hmm. But how will returning elements that aren't found in second array solve my problem? Assuming I get 3 out of 5 elements in the first array not found in the second array. So array_diff returns 3 elements, and how would I use that to solve my problem?
Since those three elements are not in the second array, that means the first array has extra stuff in it that shouldn't be there for your test to pass.
But the other two elements got matched, therefore I should show the markers that have matched at least one element. Let's say user selected 'Swimming Pool' and 'BBQ' from my features select box. So, the user wants to see all markers( houses ) that have a swimming pool and/or a bbq. Therefore if a marker doesn't have 'Swimming Pool' and 'BBQ' in their feature's array then hide it, but if a marker matches at least 'BBQ' - show it on the map. So what is the point of me getting the non-matched elements returned? I must be missing something really obvious...
Okay, so you want at least one rather than all or nothing. In that case, simply check to see if the returned length is different to the initial length. If so, then there was at least one match.
You'll need to change retArr to [] instead of {}, so that it will be an array instead of an object.
|
1

Check this library: underscore.js

And you could use _.difference(array, *others).

Or you can create your own function to check if values from one array are in another array.

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.