Skip to content Skip to sidebar Skip to footer

Using An Array To Filter An Array Javascript

I am trying to look at every value in one array and see if any of them are contained in any of the other array values. Ex: arrOne = ['a', 'b' ,'c' ]; arrTwo = ['option a', 'option

Solution 1:

This is probably not the best way of doing this, but it does work. It uses type for arrTwo and Req for ArrOne.

for(var i = (type.length - 1); i >= 0 ; i--){
    var SpliceVal = 0;

    for(var ii = 0; ii <= (Req.length -1); ii++){
        if(type[i].indexOf(Req[ii]) == -1){
            SpliceVal += 1;    
        };  
    };

    if(SpliceVal == Req.length){
        type.splice(i, 1);
    };
};

Solution 2:

There are two possible ways to address this, depending on how you lay out your arrays.

Option one -- assumes both arrays will have matching value structures

var arrOne = ['option a', 'option b', 'option c'];
var arrTwo = ['option a', 'option c', 'option b', 'option d'];
var arrFinal = [];

$(document).ready(function () {
    $.each(arrOne, function (key, value) {
        var index = $.inArray(value, arrTwo);
        if (index != -1) {
            arrFinal.push(arrTwo[index]);
            console.log(value);
        }
   });
});

Option two -- assumes both arrays will not have matching value structures (your example above)

 var arrOne = ['a', 'b', 'c'];
 var arrTwo = ['option a', 'option c', 'option b', 'option d'];
 var arrFinal = [];

 $(document).ready(function () {
     $.each(arrOne, function (key1, value1) {
         $.each(arrTwo, function (key2, value2) {
            var index = value2.indexOf(value1);
            if (index != -1) {
                arrFinal.push(value2);
                console.log(value2);
            }
         });
     });
 });

Solution 3:

Just loop through and add the ones it finds to a new array. No need for splicing.

function result(arr1, arr2) {
    var out = [];
    for (var i = 0, l = arr2.length; i < l; i++) {
        var el2 = arr2[i];
        var rel2 = el2.replace('option ', '');
        if (arr1.indexOf(rel2) > -1) { out.push(el2); }
    }
    return out;
}

result(arr1, arr2);

DEMO


Solution 4:

If you can use an third party library, you can use underscorejs, which provides lots of utility functions.

function customFilter(arrOne, arrTwo) {
    return _.filter(arrTwo, function(item){
       var res = item.substring('option '.length, item.length);   
       return _.contains(arrOne, res);
    });
}

var result = customFilter(arrOne, arrTwo);

DEMO


Solution 5:

JavaScript

Array.prototype.Filter = function(filters)
{
    var t = new Array();
    for(var i = 0; i < this.length; ++i)
    {
        for(var j = 0; j < filters.length; ++j)
        {
            if(this[i].contains(filters[j]))
            {
                t.push(this[i]);
            }
        }
    }
    return(t);
};

/* Exemple */
var filters = [ 'a', 'b' ,'c' ];
var arr = [ 'option a', 'option c', 'option b', 'option d' ];
var result = arr.Filter(filters);
alert(result);

Post a Comment for "Using An Array To Filter An Array Javascript"