I want to check if an element from list exist between two elements for another list For example I have list of ages and the other list is agesPeriod
agesPeriod : [
{
min : 10,
Max : 20
},
{
min : 30,
Max : 90
},
],
ages : [10,20,40]
Maybe this one use array.forEach and array.filter?
let agesPeriod = [
{
min : 10,
Max : 20,
},
{
min : 30,
Max : 90,
}
]
let ages = [10,20,40]
agesPeriod.forEach(arr =>{
console.log(ages.filter(i=>arr.min <= i && i<=arr.Max ))
})
const doesItExist = (list1, list2) => {
let result = false;
list1.forEach(el => {
list2.forEach(range => {
if (el >= range.min && el <= range.Max) {
result = true;
}
});
});
return result;
};
console.log(doesItExist(ages, agesPeriod));
I found the solution :
let result = []
agesPeriod.forEach(range => {
ages.forEach(el => {
if (el >= range.min && el <= range.max) {
result.push(range);
}
});
});