I have a set of data that needs to be filtered based on specific parameters.
While I can successfully filter through the initial value in the array, I am struggling to filter deeper into nested arrays. The dataset is structured as follows:
let data = [
{
exposureType: "Outdoor",
unixTime : [
// unixTime values
],
rh : [
// Relative humidity values
],
airTemp : [
// Air temperature values
],
},
{
exposureType: "Laboratory",
unixTime: [
// unixTime values for laboratory
],
rh: [
// Relative humidity values for laboratory
],
airTemp: [
// Air temperature values for laboratory
],
},
];
When I pass in four parameters like this:
sensorData("Outdoor", "airTemp", 20, 22)
I want to filter based on the first element using code like this:
let filteredArray = data.filter((val) => val.exposureType === exposureType );
Although the above code works to retrieve the object labeled "Outdoor," everything beyond this point does not work.
My goal is to access the "airTemp" array and apply filtering based on minimum and maximum values (in this case, 20 and 22). However, my attempts so far haven't been successful as it continues to return all arrays and values within that object.
I have experimented with different approaches such as:
let filteredArray = data.filter((val) => val.exposureType === exposureType && val.airTemp > min && val.airTemp < max);
filteredArray.map((element) => {
return {...element, SubElements: element.SubElements.filter((subElement) => subElement.airTemp)}
})
Unfortunately, neither of these methods has yielded the desired results.
Being new to JavaScript filtering, I am uncertain about the most effective way to achieve what I've outlined here. Can you suggest a method to accomplish this filtering task?
Your assistance is greatly appreciated.