2017-07-26 170 views
0

我有一個JSON對象10個濾鏡:json對象上的多個過濾器?

var filtro1 = function(value, selecionado){ 
    var data = JSON.parse(JSON.stringify(json.aPesquisa)); 
    var result = data.filter(function(pesquisa){ 
     ... 
    }); 
    return result; 
} 

var filtro2 = function(value, selecionado){ 
    var data = JSON.parse(JSON.stringify(json.aPesquisa)); 
    var result = data.filter(function(pesquisa){ 
     ... 
    }); 
    return result; 
} 

var filtro3 = function(horamin, horamax){ 
    var data = JSON.parse(JSON.stringify(json.aPesquisa)); 
    var result = data.filter(function(pesquisa){ 
     ... 
    }); 
    return result; 
} 

var filtro4 = function(value, selecionado){ 
    var data = JSON.parse(JSON.stringify(json.aPesquisa)); 
    var result = data.filter(function(pesquisa){ 
     ... 
    }); 
    return result; 
} 
... 

等。正如你可以看到每個過濾器單獨工作,即如果我用filtro1過濾,然後用filtro2它只顯示filtro2。

有沒有什麼辦法可以將這些過濾器放在一起?

我想過用數組與所選過濾器:

var filtros = ["filtro1, "filtro2", "filtro6", "filtro9", "filtro10"]; 

,但我不能。

一個想法和相互關聯的過濾器。

更多信息:

測試JSON:

{ 
    "aPesquisa":[ 
    { 
     "trecho":[ 
     { 
      "info":[ 
      { 
       "cor":{ 
       "nm":"Laranja" 
       }, 
       "dtPartida":"20170620 11:20", 
       "dtChegada":"20170620 16:40", 
       "hrDuracao":"03:20", 
       "vTarifa":{ 
       "tarifa":{ 
        "vlTotal":1969.17 
       } 
       } 
      } 
      ] 
     }, 
     { 
      "info":[ 
      { 
       "cor":{ 
       "nm":"Vermelho" 
       }, 
       "dtPartida":"20170620 11:20", 
       "dtChegada":"20170620 16:40", 
       "hrDuracao":"03:25", 
       "vTarifa":{ 
       "tarifa":{ 
        "vlTotal":769.90 
       } 
       } 
      }, 
      { 
       "cor":{ 
       "nm":"Vermelho" 
       }, 
       "dtPartida":"20170620 11:20", 
       "dtChegada":"20170620 16:40", 
       "hrDuracao":"03:30", 
       "vTarifa":{ 
       "tarifa":{ 
        "vlTotal":2969.20 
       } 
       } 
      }, 
      { 
       "info":[ 
       { 
        "cor":{ 
        "nm":"Azul" 
        }, 
        "dtPartida":"20170620 11:20", 
        "dtChegada":"20170620 16:40", 
        "hrDuracao":"03:20", 
        "vTarifa":{ 
        "tarifa":{ 
         "vlTotal":669.17 
        } 
        } 
       } 
       ] 
      } 
      ] 
     } 
     ] 
    } 
    ] 
} 

我10個濾鏡二:

//DURATION FILTER 
var filtroDuracao = function(horamin, horamax) 
{ 
    var data = JSON.parse(JSON.stringify(json.aPesquisa)); 
    var result = data.filter(function(item){ 
    var voos = item.trecho[0].info.filter(function(info){ 
     var time = horasParaMinutos(info.hrDuracao); 
     return time >= horamin && time <= horamax; 
    }); 
    item.trecho[0].voo = voos; 
    return voos.length > 0; 
    }); 
    return result; 
}; 


// COLOR FILTER 
var cores = []; 
var filtroCor = function(value, selecionado){ 
    var sgv = value.split('|')[0]; 

    if (selecionado) { 
    Array.prototype.push.apply(cias, json.aPesquisa.filter(function(pesquisa){ 
     var nm = pesquisa.trecho[0].info[0].cor.nm; 
     return nm == sgv; 
    })); 
    } else { 
    cias = cores.filter(function(pesquisa) { 
     var nm = pesquisa.trecho[0].info[0].cor.nm; 
     return nm != sgv; 
    }); 
    } 

    return cores; 
} 

filtroDuracao我花horamin和horamax在幾分鐘內,我從滑行隊員那裏得到。

而在filtroCor我傳遞value複選框的值以及它是否被選中或在selected爲了過濾多種顏色。

顏色複選框代碼(JavaScript的):

$(".filtros input[name='cores']").on('change', function(event) { 
    var value = $(this).val(); 
    filtroCor(value, this.checked); 
}); 

幻燈片遊俠代碼(JavaScript的):

$("#duracao-range").slider({ 
      range: true, 
      min: 0, 
      max: 1440, //1440/60 = 24 
      step: 15, 
      values: [0, 1439], 
      slide: function(event, ui) { 
       //Mínimo 
       //calcula as horas, exemplo: 125/60 = 2,083, arredonda para 2 
       var hoursMin = Math.floor(ui.values[0]/60); 
       //calcula os minutos, exemplo: 125 - 2 * 60 = 5 
       //No final terá horas = 2 e minutos = 5 
       var minutesMin = ui.values[0] - (hoursMin * 60); //restante são os minutos 
       //acrescenta o zero a esquerda se houver apenas 1 digito, 1 = 01, 2 = 02 e etc... 
       if (hoursMin.toString().length == 1) hoursMin = '0' + hoursMin; 
       if (minutesMin.toString().length == 1) minutesMin = '0' + minutesMin; 
       $(".min-duracao-label").html(hoursMin + ':' + minutesMin); 
       //Máximo 
       var hoursMax = Math.floor(ui.values[1]/60); 
       var minutesMax = ui.values[1] - (hoursMax * 60); 
       if (hoursMax.toString().length == 1) hoursMax = '0' + hoursMax; 
       if (minutesMax.toString().length == 1) minutesMax = '0' + minutesMax; 
       $(".max-duracao-label").html(hoursMax + ':' + minutesMax); 

       var horaminida= horasParaMinutos(hoursMin + ':' + minutesMin)); 
       var horamaxida= horasParaMinutos(hoursMax + ':' + minutesMax)); 
       filtroDuracao(horaminida, horamaxida); 

      } 
     }); 

horasParaMinutos功能轉換小時到分鐘:

var horasParaMinutos = function (str) { 
    var horas = str.split(':').map(Number); 
    return horas[0] * 60 + horas[1]; 
} 

我的一個懷疑和以下內容:

我有一個持續時間複選框列表與3選擇,我選擇一個顏色複選框。如何獲取持續時間和顏色複選框以進行過濾?

+3

'JSON.parse(JSON.stringify(json.aPesquisa));'...?你可以在第一個例子 –

+1

中使用'json.aPesquisa',請添加一個例子說明整個過濾是如何工作的。至少你需要存儲每個函數的兩個參數。 –

+1

@RoryMcCrossan是的,我用'JSON.parse(JSON.stringify(json.Lookup));'.filter'被替換爲'json.aLookup'返回nullo' []' – usuario

回答

0

您可以在數組中使用具有綁定參數的過濾器函數並應用一個reduce。

var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 
 
    filter1 = function(left, right, item) { 
 
     return item >= left && item <= right; 
 
    }, 
 
    filter2 = function(value, item) { 
 
     return !(item % value); 
 
    }, 
 
    filters = [filter1.bind(null, 1, 10), filter2.bind(null, 3)], 
 
    result = filters.reduce((a, fn) => a.filter(fn), array); 
 

 
console.log(result);