Javascript - Filter object array

Asked

Viewed 6,067 times

3

And I have an array of objects in javascript:

var array = 
[
    {
        conta_id : "7",
        marcar : 1,
        pag_data_emissao : "04/08/2015",
        pag_debito_credito : "D",
        pag_historico : "CHEQUE 331107  VENDA S",
        pag_id : "47782",
        pag_utilizado :"VENDA S",
        pag_valor : "7.000,00"
    },
    {
        conta_id : "7",
        marcar : 0,
        pag_data_emissao : "07/08/2015",
        pag_debito_credito : "D",
        pag_historico : "DEPOSITO 3117  VENDA X",
        pag_id : "47783",
        pag_utilizado :"VENDA X",
        pag_valor : "640,63"
    }
];

I would like to filter the array with the method. filter() where I returned all objects that have the attribute mark == 1, but only found literature and English so I could not understand how it would look.

var a = array.filter(function(obj){ obj.marcar==1; });

With the code above it does not give error but also brings me nothing.

  • 1

    missing the Return in function.

1 answer

7


the only problem is that the function you use has to return the value. Change your code to:

var filtrado = array.filter(function(obj) { return obj.marcar == 1; });

That will work.

  • opa thanks it worked even! :)

  • Dispose! Just remember to mark the answer as correct ;)

  • yes, I have to wait +10min... :( but once I release I will mark

  • I think that was my fastest answer! D

  • Sorry I had forgotten to accept that answer

Browser other questions tagged

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