0

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?

2 Answers 2

1

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

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

1 Comment

Both excellent solutions. I like that this trims the street name and sets to lowercase. One thing I don't understand (despite that the solution works) is that the low and high values are numbers and the test_array[1] and [2] are string values. I actually ran it using parseFlot(test_array[1]) ... in the code and it failed, but using it exactly as above, there was no problem.
1

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)

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.