简体   繁体   中英

Match any element of an array against another array

Using the following function, I am searching an array for the existence of a value;

var checkboxValues = ['large-car', 'small-car', 'automatic'];
var carType = ["large-car"];

function searchArray(arguments)
{
  var o = {};
  for(var i=0;i<arguments.length;i++)
  {
    o[arguments[i]]=null;
  }
  return o;
}

if (carType in searchArray(checkboxValues) )
    //do something...

This condition works well when carType (which is an array itself) contains only one value but when carType contains multiple values such as,

var carType = ["large-car", "4WD"];

...then the function will return false.

To give some background, what I am trying to do is show or hide map markers (via Google Maps) based on certain conditions,

  • Automatic
  • Manual
  • Small Car
  • Large Car
  • 4WD

Each of these values is represented as a checkbox. If "Automatic" and "Small Car" are selected, then only shown map markers who contain both those values.

If "Automatic", "Small Car" and "Large Car" are selected then only show values which match those selections.

This works if the carType array contains only a single value but as an individual vehicle may have more than one type as shown above, this is where the function fails.

What's the best way to write the function to allow for comparing multiple values in one array against that of another?

看看array_intersect从PHPJS ,PHP的的再现array_intersect在JavaScript函数。

Snippet taken from this answer .

function arrayUnique(array) {
    var a = array.concat();
    for(var i=0; i<a.length; ++i) {
        for(var j=i+1; j<a.length; ++j) {
            if(a[i] === a[j])
                a.splice(j--, 1);
        }
    }
    return a;
};

And then use it like this:

var checkboxValues = ['large-car', 'small-car', 'automatic'],
    carType = ["large-car"],
    merged = arrayUnique(checkboxValues.concat(carType));
if (merged.length === checkboxValues.length) {...}

If you need to return the matching elements of two arrays you can do this:

function matchArrays(base, toSearch) {
    var returnArray = [];
    for (var i = 0; i < toSearch.length; i++) {
        if (base.indexOf(toSearch[i]) !== -1) returnArray.push(toSearch[i]);
    }
    return returnArray;
}

Usage:

var match = matchArrays(checkboxValues, carType); // return "large-car"

You can use js functionality to match array. One ways is to use indexOf() function that return the index of the string if it is found in array or -1 if not found.

var checkboxValues = ["large-car", "small-car", "automatic"];
var carType = ["large-car","automatic","some car"];

function searchMatch(carType) {   
   var result =  new Array();   
   for(var i=0;i < carType.length;i++)   {
       // If match found push the match to the result array.
       if(checkboxValues.indexOf(carType[i]) != -1){
           result.push(carType[i])
       }
 
   }
return  result ;
}

As a result you will get ["large-car","automatic"];

Try this jQuery solution:

<script type="text/javascript">
    var checkboxValues = ['large-car', 'small-car', 'automatic'];
    var carType = ["large-car"];
    if ($.inArray(carType[0].toString(), checkboxValues ) == -1) {
        return false;// if not exists
    }
</script>

if you use underscoreJs may look like this

var checkboxValues = ['large-car', 'small-car', 'automatic'];
var carType = ['small-car','automatic'];

var result=_.any(checkboxValues,function(checkbox){
    return _.any(carType,function(carT){ return carT==checkbox;});
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM