Multiple filters on a json object?

Asked

Viewed 783 times

0

Hello.

I have 10 filters for a single json object:

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;
}
...

And so on and so forth. As you can see, each filter works separately, that is, if I filter with filtro1 and then with filtro2 it displays only the filter.

Would there be some way I could put these filters together?

I thought of using an array with the selected filters:

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

But I couldn’t.

The idea and connect the filters.

  • But are 10 filters that produce 10 results or should 10 filters that produce 1 result? As the code you presented 10 results, but with the text implies that it should be only one. Confusing.

  • you meant result = json.filter(func() .. ). filter(func2() ...)... ?

  • @Andersoncarloswoss are 10 filters that produce a result. So: I have a json and I want to filter it by color, size, quantity, price all together. The way it is I can filter each one separately. But I need only 1 result

  • Have you ever thought of, instead of json.filter in all, put the result from the previous filter? For example: var filtro2 = filtro1.filter()

  • Thus it would also be good to make an array with all filter functions apply them with a cycle, always using the result of the previous iteration

1 answer

1


first, you can structure your filters within a "Class".

var Filtro = function () {
    this.data = JSON.parse(JSON.stringify(json.aPesquisa));
}

Filtro.prototype.filtro1 = function(value, selecionado){
    this.data = this.data.filter(function(pesquisa){
        ...
    });
    return this;
}

Filtro.prototype.filtro2 = function(value, selecionado){
    this.data = this.data.filter(function(pesquisa){
        ...
    });
    return this;
}

Filtro.prototype.filtro3 = function(horamin, horamax){
    this.data = this.data.filter(function(pesquisa){
        ...
    });
    return this;
}

Filtro.prototype.filtro4 = function(value, selecionado){
    this.data = this.data.filter(function(pesquisa){
        ...
    });
    return this;
}

...

Filtro.prototype.GetResult = function() {
    return this.data;
}

you can call it as follows.:

var result = new Filtro()
    .filtro1(value1, selected1)
    .filtro2(value2, selected2)
    .filtro6(horamin, horamax) 
    .filtro9(value9, selected9) 
    .filtro9(value10, selected10) 
    .GetResult();

or as follows.:

var filtros = [
    { name: "filtro1", params: [ value1, selected1 ] }, 
    { name: "filtro2", params: [ value2, selected2 ] }, 
    { name: "filtro6", params: [ horamin, horamax ] }, 
    { name: "filtro9", params: [ value9, selected9 ] },  
    { name: "filtro10", params: [ value10, selected10 ] }
]; 
var result = filtros.reduce(function (wrapper, filtro) {
    return wrapper[filtro.name].call(wrapper, filtro.params);
}, new Filtro()).GetResult();

Follows an implementation with the same principle.

var Filtro = function () {
  this.filtros = [];
}

Filtro.prototype.filtro1 = function(value, selecionado){
  this.filtros.push({ 
    filtro: "filtro1", 
    params: {  
      value: value, 
      selecionado: selecionado
    } 
  });
  return this;
}

Filtro.prototype.filtro2 = function(value, selecionado){
  this.filtros.push({ 
    filtro: "filtro2", 
    params: {  
      value: value, 
      selecionado: selecionado
    } 
  });
  return this;
}

Filtro.prototype.filtro6 = function(horamin, horamax){
  this.filtros.push({ 
    filtro: "filtro6", 
    params: {  
      horamin: horamin, 
      horamax: horamax
    } 
  });
  return this;
}

Filtro.prototype.filtro9 = function(value, selecionado){
  this.filtros.push({ 
    filtro: "filtro9", 
    params: {  
      value: value, 
      selecionado: selecionado
    } 
  });
  return this;
}

Filtro.prototype.GetResult = function(){
  return this.filtros;
}

var result = new Filtro()
  .filtro1("value 01", "selecionado 01")
  .filtro2("value 02", "selecionado 02")
  .filtro6("horamin 06", "horamax 06") 
  .filtro9("value 09", "selecionado 09") 
  .GetResult();
console.log(result);

Browser other questions tagged

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