I have an array of timeseries objects that I need to filter in React.
Specifically, I need to return an array containing a filtered subset of the array of objects, based on the value of device_id being equal to e.g. 7F34B296.
The raw array looks as below:
[
{
"label": "time_stamp",
"data": [
"2019-04-17 21:01:25.673949957+02:00",
"2019-04-17 21:01:30.673949957+02:00",
"2019-04-17 21:01:35.673949957+02:00",
"2019-04-17 21:01:40.673949957+02:00",
"2019-04-17 21:01:45.673949957+02:00"
]
},
{
"label": "device_id",
"data": [
"7F34B296",
"7F34B296",
"7F34B296",
"AB22438D",
"AB22438D"
]
},
{
"label": "parameter_x",
"data": [
"929.1965116",
"927.5152582",
"928.7476077",
"1919.2691327",
"1918.7047619"
]
}
]
The intended output array (after filtering) looks as below:
[
{
"label": "time_stamp",
"data": [
"2019-04-17 21:01:25.673949957+02:00",
"2019-04-17 21:01:30.673949957+02:00",
"2019-04-17 21:01:35.673949957+02:00"
]
},
{
"label": "device_id",
"data": [
"7F34B296",
"7F34B296",
"7F34B296"
]
},
{
"label": "parameter_x",
"data": [
"929.1965116",
"927.5152582",
"928.7476077"
]
}
]
I tried using various methods, including below - but I seem unable to get the desired result. I think I'm missing the part on how to handle that the filtering of the entire array of objects should depend on the value of a subset of one of the objects.
const filters = [
{
predicateFn: data => data.data == "7F34B296"
}
];
function getFilteredPersons(filters) {
return datasets.filter(p => filters.every(filter => filter.predicateFn(p)));
}
console.log(getFilteredPersons(filters));
device_id, I instead include the device_id as a column in the query output and aim to filter the data once fetched to split it bydevice_id. I've not found other posts where the filtering of the entire array has to be done based on the object data of one of the arrays.filterby itself, you need tomapthe object - adding anidto each entry, thenfilterby it, eventuallymapping and removing theidadded