0

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);

3 Answers 3

1

Your first question is a simple filter

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

const result = test_array.filter( values => values[6] == street && fips.includes(values[3]))
console.log(result);

For your bonus question:

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


function matcher(array1, array2) {
    const street = array1[0];
    const fips = array1.slice(1);
    return array2[6] == street && (fips.length === 0 || fips.includes(array2[3]));
}

function array_parser(array, keywords) {
    return array.filter(values => matcher(keywords, values));
}

console.log(array_parser(test_array, ["Fernwood Avenue", "34011", "34007"]));

console.log(array_parser(test_array, ["Fernwood Avenue", "34011"]));

console.log(array_parser(test_array, ["Fernwood Avenue"]));

Sign up to request clarification or add additional context in comments.

1 Comment

All of these answers are great. I went went this one because the matcher function made the most since in the context of my larger application. Thnx all!
1

This answers the first part of the question

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

console.log(test_array.filter(e => e[6]===street && (fips.indexOf(e[3]) > -1)))

This answers the second part of the question, but I'm not sure how readable it is, to put it simply, the second parameter of Array.indexOf() specifies the starting index for searching inside the list. But if inside keyword you only specify the street, it would always return -1 since there is no second element to start the search from.

Adding a check on keyword.length solves this by always returning true when length is 1 (a.k.a, ignore the filtering by fip)

var keyword = ["Fernwood Avenue","34011"];

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

console.log(test_array.filter(e => (e[6]===keyword[0]) && (keyword.length > 1 ? keyword.indexOf(e[3],1) > -1 : true)))

Comments

1

Using two variables: you can take the function and check known indexes against each in a Array#filter(). If you want to match everything when an empty list is passed, then you can just make a mock lookup that allows everything:

function findStuff(array, street, fips) {
  const streetIndex = 6;
  const fipsIndex = 3;
  
  let fipsLookup;
  if (fips.length > 0)
    fipsLookup = new Set(fips);
  else
    fipsLookup = { has() { return true; } };

  return array.filter(item => 
    item[streetIndex] === street && fipsLookup.has(item[fipsIndex])
  )
}

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

var street = "Fernwood Avenue";
var fips = ["34011", "34007"]

let result = findStuff(test_array, street, fips);
for (const item of result)
  console.log(JSON.stringify(item)); //more concise display
  
console.log("-----");

fips = []

result = findStuff(test_array, street, fips);
for (const item of result)
  console.log(JSON.stringify(item)); //more concise display

*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.

Change the function declaration from this:

function findStuff(array, street, fips)

to this:

function findStuff(array, [street, ...fips])

In order to make use of destructuring to get the first item as street and collect the rest into an array as fips. The body of the function remains the same.

function findStuff(array, [street, ...fips]) {
  const streetIndex = 6;
  const fipsIndex = 3;
  
  let fipsLookup;
  if (fips.length > 0)
    fipsLookup = new Set(fips);
  else
    fipsLookup = { has() { return true; } };

  return array.filter(item => 
    item[streetIndex] === street && fipsLookup.has(item[fipsIndex])
  )
}

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

var keywords = ["Fernwood Avenue", "34011", "34007"];

let result = findStuff(test_array, keywords);
for (const item of result)
  console.log(JSON.stringify(item)); //more concise display
  
console.log("-----");

keywords = ["Fernwood Avenue"];

result = findStuff(test_array, keywords);
for (const item of result)
  console.log(JSON.stringify(item)); //more concise display

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.