0

I have a nested array that looks like this:

var nested_array = [
  ["18081163__,0,0.15,15238", "0", "0.15", "Somerset", "Local", "Norfolk Road"], 
  ["18081165__,0.7,0.25,15239", "0", "0.15", "Somerset", "State", "Norfolk Road"], 
  ["18081153__,0.1,0.25,15240", "0.1", "0.25", "Cumberland", "Local", "Potter Terrace"], 
  ["18081164__,1.1,2.25,15241", "1.1", "2.25", "Cumberland", "State", "Jones Street"]
]

In each subarray, index 3 = county, index 4 = road_type, and index 5 = road_name. In my application, the user must choose the road_name. I created a simple function that takes on value and returns an nested array with subarrays that contain that value:

function array_parser(old_array, key_word) {
    let new_array = [];
    for (let i = 0; i < old_array.length; i++) {
        if (old_array[i].includes(key_word)) {
            new_array.push(old_array[i]);
        }
    }
    return new_array;
}

This works well for a single value, but I want the user to be able to also choose the county and road_type optionally. If they do, I want to be a filter and return an array based on a list/array of values:

 key_words = ["Somerset", "Norfolk Road"] 

or

key_words = ["Somerset", "Local", "Norfolk Road"]

How can this be achieved?

2
  • 1
    Quick question, do these keywords not have constraints? What if there's really a road that contains the same name as the state? Will that also be a valid inclusion in the parsed array? Commented May 14, 2021 at 15:03
  • So, for road_type (index 4) those are based on a limited dropdown or choices: [State, Local, Interstate, Other]. The road_names (index 5) will all have Road, Street, Lane, etc...or will be like US-95 or something, so even if the road name is "State Street", and the road type is "State", I don't think that would be an issue (although maybe I'm wrong. Commented May 14, 2021 at 15:12

1 Answer 1

2
  • You can create a function that accepts the keywords array and the array that requires the comparison criteria.
    • It should be able to iterate over each keyword using Array#every wherein the predicate callback function uses Array#includes for the compared array against each of the iterated keyword.
  • You can modify the array_parser function to accept an array of nested arrays and an array of keywords.
    • You can use Array#filter wherein the predicate callback function will use the matcher function created previously to match the keyword criteria.

function matcher(array1, array2) {
  return array1.every(value => array2.includes(value));
}

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

var nested_array = [
  ["18081163__,0,0.15,15238", "0", "0.15", "Somerset", "Local", "Norfolk Road"],
  ["18081165__,0.7,0.25,15239", "0", "0.15", "Somerset", "State", "Norfolk Road"],
  ["18081153__,0.1,0.25,15240", "0.1", "0.25", "Cumberland", "Local", "Potter Terrace"],
  ["18081164__,1.1,2.25,15241", "1.1", "2.25", "Cumberland", "State", "Jones Street"]
];

function matcher(array1, array2) {
  return array1.every(value => array2.includes(value));
}

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

let keywords = ["Somerset", "Norfolk Road"];
console.log('keywords =', keywords);
console.log(array_parser(nested_array, keywords));


keywords = [ "Somerset", "Local", "Norfolk Road"];
console.log('keywords =', keywords);
console.log(array_parser(nested_array, keywords));
.as-console-wrapper{top: 0; max-height: unset !important;};

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

1 Comment

This is a great and streamlined solution. Works great!

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.