Array.FilterByArray function

Creating a function that filters a function based on a second function

by WizKid81

JavaScript

var arrayToFilter = [{name: "Peter", homeState: "NE"}, {name: "Sam", homeState: "IA"}, {name: "John", homeState: "NE"}, {name: "Matt", homeState: "OK"}];
var homeStateList = [{state: "Nebraska", stateCode: "NE"}, {state: "Oklahoma", stateCode: "OK"}];

//inputArray is the Array that you want ot filter
//filterArray is the array of items used to filter the input by
//filterProperty is the property within the inputArray that should be looked at to try to filter by
//lookupProperty is the property within the filterArray that is used to match to the inputArray
function filterArrayByArrayFilter(inputArray, filterArray, filterProperty, lookupProperty) {
	return inputArray.filter(function(inputElement, inputIndex){
  	var filterMatch = false;
  	filterArray.some(function(filterElement, filterIndex){
    	if (inputElement[filterProperty] === filterElement[lookupProperty]) {
      	filterMatch = true;
      }
      return filterMatch;
      });
      return filterMatch;
  });
}

var result = filterArrayByArrayFilter(arrayToFilter, homeStateList, "homeState", "stateCode");

console.log(result);