Filter through multidimensional objects with underscore.js

Asked

Viewed 218 times

1

How do I filter objects by the operations attribute inside json?

var data = Object
entrada_diff: "00:00:00"
entrada_hoje: "00:00:00"
entrada_jornada: "08:00:00"
entrada_status: "PENDENTE"
funcionario_cpf: "06547457656"
funcionario_cracha: "10703"
funcionario_foto: "06547457656.jpeg"
funcionario_marcacoes: "4"
funcionario_nome: "LARISSA"
jornada_total: "08:00:00"
operacoes: object 
 operacao1:'1',
 operacao2:'2'


refeicao_diff: "00:00:00"
refeicao_jornada: "12:00:00"
refeicao_retorno: "00:00:00"
refeicao_saida: "0"
refeicao_seg: "7200"
refeicao_status: "PENDENTE"
saida_diff: "00:00:00"
saida_hoje: "00:00:00"
saida_jornada: "18:00:00"
saida_status: "PENDENTE"
__proto__: Object
, 
Object
, 
Object
, 
Object
, 
Object
, 
Object
, 
Object
]

var set = 'operacao1'; //filtragem

    filtro =  _.filter(data, function(evt) {

    // return true where condition is true for any market
    return _.any(evt.operacoes, function(mkt,op) {

        return op == set;

        });
    });
  • 2

    I don’t see any JSON in your code. You can put an example of this JSON?

  • Put an example of JSON pf.

1 answer

1

If I understood well what you pasted (it looks like a console output), and considering that you have an array of objects, it would be like this:

var filtrado = _.filter(data, function(obj){ 
    return  obj.operacoes && obj.operacoes.operacao1 === '1';
});

var data = [{
    foo: 1,
    bar: 1,
    operacoes: {
      operacao1:'1',
      operacao2:'2'
    }
},{
    foo: 1,
    bar: 1,
    operacoes: {
      operacao1:'3',
      operacao2:'4'
    }
}, {
    foo: 1,
    bar: 1,
    operacoes: {
      operacao1:'1',
      operacao2:'3'
    }
}];

var filtrado = _.filter(data, function(obj){ 
    return  obj.operacoes && obj.operacoes.operacao1 === '1';
});

console.log(filtrado);
<script src="https://rawgithub.com/jashkenas/underscore/master/underscore-min.js"></script>

  • ok only by the 'operacao1' key would it?? look like this? Return obj.operations && obj.operacoes.filter;

  • You want to check if they have this key, is that it? What is the input data, and what is the expected output?

  • that i q return the value filtered by the key operation not the value of it..

Browser other questions tagged

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