How can I extract a subset from an array that overlaps with two specified ranges?
Let's say we have an array filled with objects:
const list = [{
id: 1,
price: "10",
weight: "19.45"
},{
id: 2,
price: "14",
weight: "27.8"
},{
id: 3,
price: "45",
weight: "65.7"
},{
id: 4,
price: "37",
weight: "120.5"
},{
id: 5,
price: "65",
weight: "26.9"
},{
id: 6,
price: "120",
weight: "19.3"
},{
id: 7,
price: "20",
weight: "45.7"
}]
We want to filter this array based on specific ranges defined for two parameters price
and weight
.
let range = {
startPrice: 15,
endPrice: 60,
startWeight: 22.0,
endWeight: 70.5,
}
The goal is to get a new array with objects that fall within both of these specified ranges. The expected result should be:
filtered = [{
id: 3,
price: "45",
weight: "65.7"
},{
id: 7,
price: "20",
weight: "45.7"
}]
Objects with id: 3
and id: 5
meet the criteria for both ranges. How should the list.filter()
function be implemented in this case? Any assistance would be highly appreciated. Thank you.