Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.filter() array using another array's elements

I have an array of people's names along with their knowledge of languages. What I want to do is pass a filter onto the language column and filter out any results that don't match.

This is the sample array

   var myArray = [["Steppen", "Spanish Polish"],
                  ["Wolf", "Spanish Polish Tagalog"],
                  ["Amanda", "Spanish"],
                  ["Ada", "Polish"],
                  ["Rhonda", "Spanish Tagalog"]];

As far as passing in filters, it could be either one language or many. Even if one language from a filter matches - the result should be returned. So for example, a filter of "Tagalog" should return - Wolf and Rhonda. A filter of "Spanish Polish" should return everyone - there's either a match in Spanish or Polish.

I wrote the filter function but for some reason it's getting stuck, when I pass the filter "Tagalog" it only iterates to the second cell in the array (Spanish Polish Tagalog) and repeats itself multiple times instead of going forward.

What am I doing wrong, should I be iterating differently?

 var userPassedFilter = new Array();
 userPassedFilter[0] = "Tagalog";

 newArray = consolidatedFilters(myArray, userPassedFilter);
 console.log(newArray);

 function consolidatedFilters(passedArray, passedFilter)
 {
 var filteredArray = passedArray.filter(    
    function(el)
    {
        for (var i = 0; i < passedArray.length; i++)
         {
            console.log("i is " + i);
             for (var j in passedFilter)
            {
                console.log("Passed Filter j " + passedFilter[j]);
                console.log("Passed Array  i " + passedArray[i][1]);        
                console.log("String Search " + passedArray[i][1].search(passedFilter[j]));

                if (passedArray[i][1].search(passedFilter[j]) != -1)
                {
                    return true;
                }
            }           
        }
         return false;
    }
 );     
 return filteredArray;
 }
like image 665
firedrawndagger Avatar asked Sep 08 '11 20:09

firedrawndagger


People also ask

How do you filter an array from all elements of another array object?

One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.

How can we filter an array of elements from a given array in ES6?

ES6 | Array filter() Method Callback: The function is a predicate, to test each element of the array. Return true to keep the element, false otherwise. It accepts three arguments: element: The current element being processed in the array.

How do I filter two arrays?

const arr1 = [4, 23, 7, 6, 3, 6, 4, 3, 56, 4]; const arr2 = [4, 56, 23]; We are required to write a JavaScript function that takes in these two arrays and filters the first to contain only those elements that are not present in the second array. And then return the filtered array.


2 Answers

this is the ultimate solution in the ES6 way: No need to Search the same query again in another thread.

var array1 = ['a', 'b', 'c', 'd', 'e'];
var array2 = ['b', 'd', 'f'];

array1 = array1.filter(function(item) {
  return !array2.includes(item); 
})
console.log(array1); // [ 'a', 'c', 'e' ]
console.log(array2); // [ 'b', 'd', 'f' ]
like image 174
Debabrata Nayak Avatar answered Nov 02 '22 06:11

Debabrata Nayak


To me it seems like you're making it a little too complicated.

  1. Iterating three times (filter, for loop, for in loop).
  2. Using a for in loop for an array.
  3. Using both new Array and [...].

I updated it a little and it looks like this is what you want: http://jsfiddle.net/pimvdb/RQ6an/.

var myArray = [["Steppen", "Spanish Polish"],
              ["Wolf", "Spanish Polish Tagalog"],
              ["Amanda", "Spanish"],
              ["Ada", "Polish"],
              ["Rhonda", "Spanish Tagalog"]];

var userPassedFilter = ["Tagalog"];

newArray = consolidatedFilters(myArray, userPassedFilter);
console.log(newArray);

function consolidatedFilters(passedArray, passedFilter) {
    var filteredArray = passedArray.filter(
    function(el) { // executed for each person
        for (var i = 0; i < passedFilter.length; i++) { // iterate over filter
            if (el[1].indexOf(passedFilter[i]) != -1) {
                return true; // if this person knows this language
            }
        }
        return false;
    }
    );     
    return filteredArray;
}
like image 38
pimvdb Avatar answered Nov 02 '22 07:11

pimvdb