I have a List of ages and a List of ageRanges, using javascript I am tying to filter the ages that fall within any of the ageRanges
I got code working for a single ageRange, but how can I do this if ageRanges is an array?
Example of code I used with a single ageRange:
var ages = [32, 33, 16, 22, 40];
var ageRanges = {min: 10, max:23};
function checkAdult(age) {
return age >= ageRanges.min && age <= ageRanges.max;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.filter(checkAdult);
} //returns 16,22
Example of the data I want to filter:
var ages = [32, 33, 16, 22, 40];
var ageRanges = [{min: 10, max:23},{min:30, max:36},{min:44, max:49}];
I tried doing a for loop, but struggled to get it right as I'm definitely new to all this so any help would be appreciated. Also started to wonder if a for loop was even the riI can easily format the ageRanges data differently if there is a better way to represent a list of ranges
question from:
https://stackoverflow.com/questions/65830385/filter-list-of-numbers-using-a-list-of-ranges 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…