Reputation: 1923
I have a nested array that has subarrays with a length of 6 and string values. I also have 2 variables that have float values as below:
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 pt_one = 0.1; var pt_two = 0.59; var street = "Fernwood Avenue";
I want to use pt_one and pt_two as a low-high range. I want to iterate through the subarrays and create a new array where subarray[1] is equal to or great than pt_one and subarray[2] is less than or equal to pt_two and subarray[6] = street and push that array to a new_array. For example, the output in this scenario would be:
new_array =[["02271163", "0.1", "0.22", "34003", "Bergen", "Local", "Fernwood Avenue"],
["04351186", "0.3", "0.59", "34007", "Camden", "Local", "Fernwood Avenue"]];
How can I achieve this?
Upvotes: 0
Views: 62
Reputation: 23654
Using filter
, this also checks for type
number on the comparisons and capitalization/whitespace issues on street.
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 findIt(low, high, street) {
return test_array.filter(el =>
+el[1] >= low && +el[2] <= high && el[6].trim().toLowerCase() === street.trim().toLowerCase());
}
var pt_one = 0.1;
var pt_two = 0.59;
var street = "Fernwood Avenue";
console.log(findIt(pt_one, pt_two, street))
Upvotes: 1
Reputation: 171679
This is a basic filter() operation where you check each of the conditions
var pt_one = 0.1; var pt_two = 0.59; var street = "Fernwood Avenue";
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 res = test_array.filter(arr=>{
return arr[6] === street && arr[1] >= pt_one && arr[2] <= pt_two;
});
console.log(res)
Upvotes: 1