Problem with JSON filter and Array

0

I'm trying to filter a JSON with category but I'm not getting it.

I have 3 checkboxes and when I select them they should filter a json as selected. and when I uncheck a checkbox it should filter only those that are selected. If no checkbox is selected it should return empty [] .

And when I select a checkbox and uncheck it it only returns [] . Follow the code in JSFiddle

    
asked by anonymous 11.07.2017 / 20:04

1 answer

1

Assuming that it is necessary to filter the sections that contain the flight of the selected company, you could simplify the filter to check for flights in the selected sections:

function filtro(valor, selecionado) {

    json.aPesquisa.forEach(function(item) {

        if (selecionado) {
            item.trecho.forEach(function(trecho) {
                trecho.voo.forEach(function(voo) {
                    if (voo.cia.nm == valor) {
                        categoriaList.push(voo);
                    }
                })
            })
        }
    })

    $("#resultado").html(JSON.stringify(categoriaList));
}

JSFiddle: link

    
11.07.2017 / 20:35