I have this code that filters a JSON object:
var filtrar = function (horamin, horamax)
{
var data = JSON.parse(JSON.stringify(json.aPesquisa));
let result = data.filter(item => {
let voos = item.trecho[1].voo.filter(voo => {
var time = horasParaMinutos(voo.hrDuracao);
console.log(horamin);
return time >= horamin && time <= horamax;
});
item.trecho[1].voo = voos;
return voos.length > 0;
});
return result;
};
This code filters out the trecho[1].voo
. My problem is that when it returns false or with no data it does not display trecho[0].voo
.
I'll leave you an example in the JSFiddle to better understand.
Example if the slider is selected the minimum time of 08:10 it returns this json:
[
{
"dsObservacao": null,
"trecho": [
{
"sqTrecho": 1,
"voo": [
{
"dtPartida": "20170720 11:20",
"dtChegada": "20170720 16:40",
"hrDuracao": "00:30"
}
]
},
{
"sqTrecho": 2,
"voo": [
{
"dtPartida": "20170727 14:15",
"dtChegada": "20170727 17:40",
"hrDuracao": "08:10"
}
]
}
]
}
]
If the time is greater than 08:10 it does not return anything []
when it was to return like this:
[
{
"dsObservacao": null,
"trecho": [
{
"sqTrecho": 1,
"voo": [
{
"dtPartida": "20170720 11:20",
"dtChegada": "20170720 16:40",
"hrDuracao": "00:30"
}
]
}
]
}
]
Summarizing : What do I need to do and filter only trecho[1]
and keep trecho[0]
.