Filter an object array within another object array

Asked

Viewed 1,334 times

2

I have an array like the one below and would like to filter it by the property that is inside an object inside an array that is inside another object.

The idea would be to return all objects whose movements are like: "exit".

In another question on the forum I found this code and adapted it for my circumstance :) Their solution was:

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

How would I filter for moves that are like, "Exit"

    var array = 
    [
        {
            conta_id : "7",
            movimentações: { InternaMov : [
               {data: "20/10/1029",
                tipo: "saida",
                valor: "300,00"},
               {data: "19/10/1029",
                tipo: "entrada",
                valor: "900,00"}
            ]},
            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",
            movimentações: { InternaMov : [
               {data: "20/10/1029",
               tipo: "saida",
               valor: "300,00"},
               {data: "19/10/1029",
               tipo: "entrada",
               valor: "900,00"}
            ]},
            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"
        }
    ];
  • But you want to return only the innermost object or you want to return the information from the outer and the filtered inner object?

1 answer

2

There are two solutions to solve your problem. The first is if you just want the drives, then you would do it as follows:

array.map(obj1 => (obj1.movimentacoes.InternaMov.filter(obj2 => obj2.tipo === 'saida')));

var array = 
    [
        {
            conta_id : "7",
            movimentacoes: { InternaMov : [
               {data: "20/10/1029",
                tipo: "saida",
                valor: "300,00"},
               {data: "19/10/1029",
                tipo: "entrada",
                valor: "900,00"}
            ]},
            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",
            movimentacoes: { InternaMov : [
               {data: "20/10/1029",
               tipo: "saida",
               valor: "300,00"},
               {data: "19/10/1029",
               tipo: "entrada",
               valor: "900,00"}
            ]},
            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"
        }
    ];
    
console.log(array.map(obj1 => (obj1.movimentacoes.InternaMov.filter(obj2 => obj2.tipo === 'saida')))); 

If you want to keep the information of the external object, then you must make the following change in the map:

array.map(obj1 => ({...obj1, movimentacoes: obj1.movimentacoes.InternaMov.filter(obj2 => obj2.tipo === 'saida')}))

var array = 
    [
        {
            conta_id : "7",
            movimentacoes: { InternaMov : [
               {data: "20/10/1029",
                tipo: "saida",
                valor: "300,00"},
               {data: "19/10/1029",
                tipo: "entrada",
                valor: "900,00"}
            ]},
            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",
            movimentacoes: { InternaMov : [
               {data: "20/10/1029",
               tipo: "saida",
               valor: "300,00"},
               {data: "19/10/1029",
               tipo: "entrada",
               valor: "900,00"}
            ]},
            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"
        }
    ];

console.log(array.map(obj1 => ({...obj1, movimentacoes: obj1.movimentacoes.InternaMov.filter(obj2 => obj2.tipo === 'saida')})));

Note that in the first example we only filter the internal arrays, while in the second example we copy all the properties of the object inside array and filter only the internal arrays.

  • 1

    Thanks Felipe Avelar... Thanks a lot.

  • @Alessandrocunhalorenz if this solution solved your problem, could you mark as answer, please?

Browser other questions tagged

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