I need help with the filter in Json with jQuery, I have this code:
var chamaFiltro = function(horaminida){
var pesquisa = {
idamin: horasParaMinutos(horaminida)
};
var filtrados = json.aPesquisa.filter(function(voo) {
voo = JSON.parse(JSON.stringify(voo));
voo.trecho = voo.trecho.filter(function(trecho){
trecho = JSON.parse(JSON.stringify(trecho));
trecho.voo = trecho.voo.filter(function(voos){
return horasParaMinutos(voos.dtPartida.slice(9, 14)) >= pesquisa.idamin;
});
return trecho.voo.length > 0;
});
return voo.trecho.length > 0;
});
console.log(filtrados);
};
It filters a JSON
and returns me the time is greater than the selected time in a slide range
.
If you put one:
console.log (excerpt.voo);
Instead of:
return stretch.voo.length > 0;
It is noticed in the console that it normally filters but when I try to give a return
it returns all the data until those that are smaller than the time of slide range
.
Does anyone know how I can return only the filtered data?
When calling the function, does it only return the filtered data?
Flame (Horaminida);
Json:
{
"aPesquisa":[
{
"dsObservacao":null,
"trecho":[
{
"sqTrecho":1,
"voo":[
{
"dtPartida":"20170620 11:20",
"dtChegada":"20170620 16:40"
}
]
},
{
"sqTrecho":2,
"voo":[
{
"dtPartida":"20170627 04:10",
"dtChegada":"20170627 07:40"
},
{
"dtPartida":"20170627 14:15",
"dtChegada":"20170627 17:40"
}
]
}
]
},
{
"dsObservacao":null,
"trecho":[
{
"sqTrecho":1,
"voo":[
{
"dtPartida":"20170720 11:20",
"dtChegada":"20170720 16:40"
}
]
},
{
"sqTrecho":2,
"voo":[
{
"dtPartida":"20170727 04:10",
"dtChegada":"20170727 07:40"
},
{
"dtPartida":"20170727 14:15",
"dtChegada":"20170727 17:40"
}
]
}
]
}
]
}
Assuming I select 10:30 in the slide range
it should return me only where the dtPartida(dtPartida.slcie(9, 14))
time is greater than the selected time in slide range
in case 10:30. the return would look like this:
[
{
"dsObservacao":null,
"trecho":[
{
"sqTrecho":1,
"voo":[
{
"dtPartida":"20170620 11:20",
"dtChegada":"20170620 16:40"
}
]
},
{
"sqTrecho":2,
"voo":[
{
"dtPartida":"20170627 14:15",
"dtChegada":"20170627 17:40"
}
]
}
]
},
{
"dsObservacao":null,
"trecho":[
{
"sqTrecho":1,
"voo":[
{
"dtPartida":"20170720 11:20",
"dtChegada":"20170720 16:40"
}
]
},
{
"sqTrecho":2,
"voo":[
{
"dtPartida":"20170727 14:15",
"dtChegada":"20170727 17:40"
}
]
}
]
}
]
Follow the code on JSFiddle