2

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?

userabuser
  • 433
  • 5
  • 18
  • `arguments` is a special value in functions, you shouldn't use it as a variable name. – Niet the Dark Absol Jun 17 '13 at 11:10
  • 1
    Well you will need two loops, nested into each other – one that goes over your items, in and that one that compares the current item against all search values. – CBroe Jun 17 '13 at 11:10

5 Answers5

1

Take a look at array_intersect from PHPJS, a reproduction of PHP's array_intersect function in JavaScript.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

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"
Community
  • 1
  • 1
Niccolò Campolungo
  • 11,824
  • 4
  • 32
  • 39
  • Thanks for your reply. If `carType` contains any other value not shown in the `checkboxvalues` array then the condition fails. The array may contain for example `["large-car", "SUV"]`, if one of the values matches I need to return the value that was found, the other non-matched value can be discarded. – userabuser Jun 17 '13 at 11:24
  • Appreciate your edit mate. If I supply `["large-car", "small-car"]` then only the first result is returned and the second is not. If I supply `["large-head", "small-car"]` then `small-car` is returned. The condition needs to return each value if they are matched. So it's only returning the first correct value it finds and not there after. – userabuser Jun 17 '13 at 11:40
  • It was easy to modify, now it should do the job. – Niccolò Campolungo Jun 17 '13 at 11:43
1

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"];

Alexander Gorelik
  • 3,985
  • 6
  • 37
  • 53
0

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>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sangram parmar
  • 8,462
  • 2
  • 23
  • 47
0

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;});
});
D__
  • 141
  • 1
  • 5