@acwoss/

Filtro de dados com JavaScript

JavaScript

https://pt.stackoverflow.com/q/213822/5878

fork
loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.7571618499854156","path":"main.js","file":{"path":"main.js","content":{"asEncoding":{"base64":"Y29uc3QganNvbiA9IHsNCiAgICJhUGVzcXVpc2EiOlsNCiAgICAgIHsNCiAgICAgICAgICJkc09ic2VydmFjYW8iOm51bGwsDQogICAgICAgICAidHJlY2hvIjpbDQogICAgICAgICAgICB7DQogICAgICAgICAgICAgICAic3FUcmVjaG8iOjEsDQogICAgICAgICAgICAgICAidm9vIjpbDQogICAgICAgICAgICAgICAgICB7DQogICAgICAgICAgICAgICAgICAgICJkdFBhcnRpZGEiOiIyMDE3MDYyMCAxMToyMCIsDQogICAgICAgICAgICAgICAgICAgICJkdENoZWdhZGEiOiIyMDE3MDYyMCAxNjo0MCINCiAgICAgICAgICAgICAgICAgIH0NCiAgICAgICAgICAgICAgIF0NCiAgICAgICAgICAgIH0sDQogICAgICAgICAgICB7DQogICAgICAgICAgICAgICAic3FUcmVjaG8iOjIsDQogICAgICAgICAgICAgICAidm9vIjpbDQogICAgICAgICAgICAgICAgICB7DQogICAgICAgICAgICAgICAgICAgICANCiAgICAgICAgICAgICAgICAgICAgICJkdFBhcnRpZGEiOiIyMDE3MDYyNyAwNDoxMCIsDQogICAgICAgICAgICAgICAgICAgICAiZHRDaGVnYWRhIjoiMjAxNzA2MjcgMDc6NDAiDQogICAgICAgICAgICAgICAgICB9LA0KICAgICAgICAgICAgICAgICAgew0KICAgICAgICAgICAgICAgICAgICAgImR0UGFydGlkYSI6IjIwMTcwNjI3IDE0OjE1IiwNCiAgICAgICAgICAgICAgICAgICAgICJkdENoZWdhZGEiOiIyMDE3MDYyNyAxNzo0MCINCiAgICAgICAgICAgICAgICAgIH0NCiAgICAgICAgICAgICAgIF0NCiAgICAgICAgICAgIH0NCiAgICAgICAgIF0NCiAgICAgIH0sDQogICAgICANCiAgICAgIHsNCiAgICAgICAgICJkc09ic2VydmFjYW8iOm51bGwsDQogICAgICAgICAidHJlY2hvIjpbDQogICAgICAgICAgICB7DQogICAgICAgICAgICAgICAic3FUcmVjaG8iOjEsDQogICAgICAgICAgICAgICAidm9vIjpbDQogICAgICAgICAgICAgICAgICB7DQogICAgICAgICAgICAgICAgICAgICJkdFBhcnRpZGEiOiIyMDE3MDcyMCAxMToyMCIsDQogICAgICAgICAgICAgICAgICAgICJkdENoZWdhZGEiOiIyMDE3MDcyMCAxNjo0MCINCiAgICAgICAgICAgICAgICAgIH0NCiAgICAgICAgICAgICAgIF0NCiAgICAgICAgICAgIH0sDQogICAgICAgICAgICB7DQogICAgICAgICAgICAgICAic3FUcmVjaG8iOjIsDQogICAgICAgICAgICAgICAidm9vIjpbDQogICAgICAgICAgICAgICAgICB7DQogICAgICAgICAgICAgICAgICAgICANCiAgICAgICAgICAgICAgICAgICAgICJkdFBhcnRpZGEiOiIyMDE3MDcyNyAwNDoxMCIsDQogICAgICAgICAgICAgICAgICAgICAiZHRDaGVnYWRhIjoiMjAxNzA3MjcgMDc6NDAiDQogICAgICAgICAgICAgICAgICB9LA0KICAgICAgICAgICAgICAgICAgew0KICAgICAgICAgICAgICAgICAgICAgImR0UGFydGlkYSI6IjIwMTcwNzI3IDE0OjE1IiwNCiAgICAgICAgICAgICAgICAgICAgICJkdENoZWdhZGEiOiIyMDE3MDcyNyAxNzo0MCINCiAgICAgICAgICAgICAgICAgIH0NCiAgICAgICAgICAgICAgIF0NCiAgICAgICAgICAgIH0NCiAgICAgICAgIF0NCiAgICAgIH0NCiAgIF0NCn07DQoNCg0KZnVuY3Rpb24gY2hhbWFGaWx0cm8gKGhvcmFtaW5pZGEpDQp7DQogIC8vIENvcGlhIG9zIGRhZG9zIHBhcmEgYGRhdGFgOg0KICBsZXQgZGF0YSA9IE9iamVjdC5hc3NpZ24oe30sIGpzb24pOw0KICANCiAgLy8gRmlsdHJhIG9zIGRhZG9zIGVtIGBhUGVzcXVp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ICANCiAgLy8gUmV0b3JuYSBvIHJlc3VsdGFkbzoNCiAgcmV0dXJuIHJlc3VsdDsNCn0NCg0KDQpjb25zb2xlLmxvZyhjaGFtYUZpbHRybygiMTA6MzAiKSk7"},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const 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"
                  }
               ]
            }
         ]
      }
   ]
};


function chamaFiltro (horaminida)
{
  // Copia os dados para `data`:
  let data = Object.assign({}, json);
  
  // Filtra os dados em `aPesquisa`:
  let result = data.aPesquisa.filter(item => {
    
    // Filtra os dados em `aPesquisa[i].trecho`:
    let trechos = item.trecho.filter(trecho => {

      let voos = trecho.voo.filter(voo => {

        // Extrai o horário de `dtPartida`:
        let time = voo.dtPartida.split(' ')[1];

        // Mantém no resultado final se o horário for maior ou igual ao de entrada:
        return time > horaminida;

      });

      // Atualiza os valores filtrados dos voos:
      trecho.voo = voos;

      // Mantém no resultado final se possuir dados em voo:
      return voos.length > 0;

    });

    // Atualiza os valores filtrados dos trechos:
    item.trecho = trechos;

    // Mantém no resultado final se possuir dados em trecho:
    return trechos.length > 0;
    
  });
  
  // Retorna o resultado:
  return result;
}


console.log(chamaFiltro("10:30"));
Native Browser JavaScript