Problem with JSON filter and Array

Asked

Viewed 70 times

0

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

I have 3 checkbox and when I select them I should filter a json as selected. and when unchecking a checkbox 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 returns only []. Follow the code on Jsfiddle

1 answer

1


Assuming that it is necessary to filter the stretches that contain the flight of the selected company, it could simplify the filter to check if there are flights in the stretches that are selected:

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: https://jsfiddle.net/lbclucascosta/xrkuoqhq/84/

  • and your code when I select the checkbox of tam it returns the flight.cia.nm with the name tam and passed. it would be possible for him to return only the da tam if only it is selected?

  • see now please @EGDEV

  • 1

    It worked perfect. Thank you very much!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.