Consider the scenario of having 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"]
];
We also have a small array containing string values. This is just a simplified example, there could be more values:
var string_values = ["0.1", 4.2"]
The objective here is to filter or parse the array in such a way that we only get the subarrays where the value at index 1 matches any of the string_values. The current approach works but is somewhat cumbersome. Is there a more efficient method using .filter or .find (or any other single line technique) 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);