Lets say I have a nested array:
var test_array = [
["0", "0.1", "4.2", "Kramer Street"],
["1", "0.2", "3.5", "Lamar Avenue"],
["3", "4.2", "7.1", "Kramer Street"]
];
I also have a small array of string values. This is a simplified example as there could be a single value or multiple values:
var string_values = ["0.1", 4.2"]
I want to filter or parse the array to just get the subarrays where index 1 and only index 1 is equal to either of my string_values. I've tried the following approach but its cumbersome (although it works). Is there a better way using .filter or .find (or another single line method) to achieve this?
var test_array = [
["0", "0.1", "4.2", "Kramer Street"],
["1", "0.2", "3.5", "Lamar Avenue"],
["3", "4.2", "7.1", "Kramer Street"]
];
var string_values = ["0.1", "4.2"]
var new_array = [];
for (let i=0; i < test_array.length; i++) {
if (string_values.indexOf(test_array[i][1]) !== -1) {
new_array.push(test_array[i]);
}
}
console.log(new_array);