1
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 only the trecho[1].voo
. My problem is that when it returns false or without any data it does not display the trecho[0].voo
.
I’ll leave an example on Jsfiddle to better understand.
Example if the slide ranger 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 8:10 he returns nothing []
when it was to return like this:
[
{
"dsObservacao": null,
"trecho": [
{
"sqTrecho": 1,
"voo": [
{
"dtPartida": "20170720 11:20",
"dtChegada": "20170720 16:40",
"hrDuracao": "00:30"
}
]
}
]
}
]
In short: What I need to do and filter only the trecho[1]
and maintain the trecho[0]
.
And why should he return
trecho[0].voo
if the filter is applied only intrecho[1].voo
? By the way, it’s good you always refer to discussion that generated the code in question as it may be useful for those who will answer. For example, in this question you did not put what is the functionhorasParaMinutos
. Incidentally, what is the difference between the questions exactly?– Woss
@Andersoncarloswoss this filter filters only the object
trecho[1].voo
. In thetrecho[0].voo
also has data but is not being filtered. The problem is that when the filter does not find any object that is within the hour selects it returns[]
and does not return the data that is intrecho[0].voo
. Or if there is no data intrecho[1].voo
he does not display thetrecho[0].voo
. thehorasParaMinutos
and only to convert the hour to minute to be able to check if it is greater or less than the time received by the parameters horamin and horamax which is in minutes– usuario
Yes, because you are applying the filter to all elements based on the value of
trecho[1]
. If it has no value, it returns false for all positions in the list and it is null. You should only apply the filter usingtrecho[1]
whenitem
refer to section 1.– Woss
Dude, it got a little confused what you want... you want to filter only the stretch[1] and, if it didn’t work, show the stretch[0] or you want to filter all the passages and, if it didn’t work, show the stretch[0] ??
– guijob
@J.Guilherme I want to filter only the stretch[1] and keep the stretch[0].
– usuario
You want to filter the duration of the trip (
hrDuracao
), that’s it?– Sergio
@Sergio I want to filter the
hrDuracao
only oftrecho[1]
and maintain thetrecho[0]
.– usuario
Okay, so you’ll have a slider lasting (0 to type 24h) and then filter, right?
– Sergio
edited my answer, see if it works as expected
– guijob
@Sergio yes. a slider from 0 to 24h and then filter only the stretch[1]. flight
– usuario