Suppose I have an array and 2 variables; one that's just a simple string, and one that's an array of string values:
var street = "Fernwood Avenue";
var fips = ["34011", "34007"];
var test_array =[["06101123", "0", "0.2", "34011", "Cumberland", "Local", "Fernwood Avenue"],
["02271163", "0.1", "0.22", "34003", "Bergen", "Local", "Fernwood Avenue"],
["04351186", "0.3", "0.59", "34007", "Camden", "Local", "Fernwood Avenue"],
["07131150", "0", "0.3", "34013", "Essex", "Local", "Beacon Street"],
["03041026", "0", "0.13", "34005", "Burlington", "Local", "Beacon Street"],
["20121109", "0.18", "0.43", "34039", "Union", "Local", "Jones Lane"],
["15141139", "0", "0.27", "34029", "Ocean", "Local", "Jones Lane"]];
What I want to do here is iterate through my test_array and find just the subarrays that match for street in sub_array[6] AND have a match for sub_array[3] in the fips list (["34011", "34007"]). Is there a way to use filter (or any other method) to achieve this and return the results below?
var new_array = [["06101123", "0", "0.2", "34011", "Cumberland", "Local", "Fernwood Avenue"],
["04351186", "0.3", "0.59", "34007", "Camden", "Local", "Fernwood Avenue"]];
*Bonus: Suppose there is a function where the street and fips come in as a list argument where the street name will ALWAYS be there but the fips values may or may not. So,
var keyword = ["Fernwood Avenue", "34011", "34007"];
or
var keyword = ["Fernwood Avenue"];
or
var keyword = ["Fernwood Avenue", "34011"];
with a function(s) something like this (*This does not work for scenario 1)
function matcher(array1, array2) {
return array1.every(value => array2.includes(value));
}
function array_parser(array, keywords) {
return array.filter(values => matcher(keywords, values));
}
var new_array = array_parser(test_array, keywords);