Filter an array within another array (Ionic)

Asked

Viewed 30 times

0

Hello I started studying Ionic a little while ago and I have the following problem. I have the following ARRAY:

"ordserv": [
    {
        "codemp": "01",
        "numos": "0000000001",
        "codcli": "000001",
        "datcadast": "2020-05-19T00:00:00.000Z",
        "user_id": "INFTEC",
        "staos": "7",
        "descricao": "Teste",
        "codtabpreco": null,
        "codcpagto": null,
        "subgrupo": null,
        "resptecnico": null,
        "cli_respfinanc": null,
        "datprometida": "2020-12-31T00:00:00.000Z",
        "datresposta": null,
        "resp_resposta": null,
        "numpedcli": null,
        "resesperado": null,
        "avaliacaofinal": null,
        "razsoccli": "EMPRESA USUÁRIA DO SISTEMA",
        "numcgccpfcli": "00000000000000",
        "descpagto": null,
        "razsoccli_respfinanc": null,
        "Equipamento": [
            {
                "codemp": "01",
                "numos": "0000000001",
                "numequip": "001",
                "seriequip": "SER-A001",
                "codmat": "0121000003",
                "numnf": null,
                "sernf": null,
                "codforn": null,
                "numitemnf": null,
                "desmat": "CEO GP4 24 FO PARA INSTALACAO AEREA",
                "razsocforn": null,
                "numcgccpf": null
            }
        ],        
    {
        "codemp": "01",
        "numos": "0000000002",
        "codcli": "000002",
        "datcadast": "2020-05-19T00:00:00.000Z",
        "user_id": "INFTEC",
        "staos": "1",
        "descricao": "Teste 2",
        "codtabpreco": null,
        "codcpagto": null,
        "subgrupo": null,
        "resptecnico": null,
        "cli_respfinanc": null,
        "datprometida": "2020-12-31T00:00:00.000Z",
        "datresposta": null,
        "resp_resposta": null,
        "numpedcli": null,
        "resesperado": null,
        "avaliacaofinal": null,
        "razsoccli": "VIANET TELECOMUNICACOES E INTERNET LTDA",
        "numcgccpfcli": "05884946000181",
        "descpagto": null,
        "razsoccli_respfinanc": null,
        "Equipamento": [
            {
                "codemp": "01",
                "numos": "0000000002",
                "numequip": "001",
                "seriequip": "SER-A002",
                "codmat": 0121000002,
                "numnf": null,
                "sernf": null,
                "codforn": null,
                "numitemnf": null,
                "desmat": null,
                "razsocforn": null,
                "numcgccpf": null
            }
        ],
    {
        "codemp": "01",
        "numos": "0000000003",
        "codcli": "000001",
        "datcadast": "2020-05-21T14:50:06.987Z",
        "user_id": "INFTEC",
        "staos": "3",
        "descricao": "Teste",
        "codtabpreco": null,
        "codcpagto": null,
        "subgrupo": null,
        "resptecnico": null,
        "cli_respfinanc": null,
        "datprometida": "2020-12-31T00:00:00.000Z",
        "datresposta": null,
        "resp_resposta": null,
        "numpedcli": null,
        "resesperado": null,
        "avaliacaofinal": null,
        "razsoccli": "EMPRESA USUÁRIA DO SISTEMA",
        "numcgccpfcli": "00000000000000",
        "descpagto": null,
        "razsoccli_respfinanc": null,
        "Equipamento": [
            {
                "codemp": "01",
                "numos": "0000000003",
                "numequip": "001",
                "seriequip": "SER-A001",
                "codmat": "0121000001",
                "numnf": null,
                "sernf": null,
                "codforn": null,
                "numitemnf": null,
                "desmat": "CEO GP4 24 FO PARA INSTALACAO AEREA",
                "razsocforn": null,
                "numcgccpf": null
            }
        ],           

    }
]

I’m trying to filter the ordserv that has in Equipment the codmat I entered into the system. The return I wanted another array but only with the filtered ordserv.

I tried the following code I’ve been researching but failed:

this.ordserv = this.ordserv.filter((f) => f.Equipamento.find(j => j.codmat == this.codmat))

Who can help me thank you so much.

1 answer

0

In this case, as you need to search an array within an array, you need to do another filter to search in "Equipment", but in this case, you have "some" equipment with the desired codmat, so we can use some instead of filter. Look at this example:

var obj = {
  "ordserv": [
    {
      "codemp": "01",
      "numos": "0000000001",
      "codcli": "000001",
      "datcadast": "2020-05-19T00:00:00.000Z",
      "user_id": "INFTEC",
      "staos": "7",
      "descricao": "Teste",
      "codtabpreco": null,
      "codcpagto": null,
      "subgrupo": null,
      "resptecnico": null,
      "cli_respfinanc": null,
      "datprometida": "2020-12-31T00:00:00.000Z",
      "datresposta": null,
      "resp_resposta": null,
      "numpedcli": null,
      "resesperado": null,
      "avaliacaofinal": null,
      "razsoccli": "EMPRESA USUÁRIA DO SISTEMA",
      "numcgccpfcli": "00000000000000",
      "descpagto": null,
      "razsoccli_respfinanc": null,
      "Equipamento": [
        {
          "codemp": "01",
          "numos": "0000000001",
          "numequip": "001",
          "seriequip": "SER-A001",
          "codmat": "0121000003",
          "numnf": null,
          "sernf": null,
          "codforn": null,
          "numitemnf": null,
          "desmat": "CEO GP4 24 FO PARA INSTALACAO AEREA",
          "razsocforn": null,
          "numcgccpf": null
        }
      ]
    },
    {
      "codemp": "01",
      "numos": "0000000002",
      "codcli": "000002",
      "datcadast": "2020-05-19T00:00:00.000Z",
      "user_id": "INFTEC",
      "staos": "1",
      "descricao": "Teste 2",
      "codtabpreco": null,
      "codcpagto": null,
      "subgrupo": null,
      "resptecnico": null,
      "cli_respfinanc": null,
      "datprometida": "2020-12-31T00:00:00.000Z",
      "datresposta": null,
      "resp_resposta": null,
      "numpedcli": null,
      "resesperado": null,
      "avaliacaofinal": null,
      "razsoccli": "VIANET TELECOMUNICACOES E INTERNET LTDA",
      "numcgccpfcli": "05884946000181",
      "descpagto": null,
      "razsoccli_respfinanc": null,
      "Equipamento": [
        {
          "codemp": "01",
          "numos": "0000000002",
          "numequip": "001",
          "seriequip": "SER-A002",
          "codmat": "0121000002",
          "numnf": null,
          "sernf": null,
          "codforn": null,
          "numitemnf": null,
          "desmat": null,
          "razsocforn": null,
          "numcgccpf": null
        }
      ]
    },
    {
      "codemp": "01",
      "numos": "0000000003",
      "codcli": "000001",
      "datcadast": "2020-05-21T14:50:06.987Z",
      "user_id": "INFTEC",
      "staos": "3",
      "descricao": "Teste",
      "codtabpreco": null,
      "codcpagto": null,
      "subgrupo": null,
      "resptecnico": null,
      "cli_respfinanc": null,
      "datprometida": "2020-12-31T00:00:00.000Z",
      "datresposta": null,
      "resp_resposta": null,
      "numpedcli": null,
      "resesperado": null,
      "avaliacaofinal": null,
      "razsoccli": "EMPRESA USUÁRIA DO SISTEMA",
      "numcgccpfcli": "00000000000000",
      "descpagto": null,
      "razsoccli_respfinanc": null,
      "Equipamento": [
        {
          "codemp": "01",
          "numos": "0000000003",
          "numequip": "001",
          "seriequip": "SER-A001",
          "codmat": "0121000001",
          "numnf": null,
          "sernf": null,
          "codforn": null,
          "numitemnf": null,
          "desmat": "CEO GP4 24 FO PARA INSTALACAO AEREA",
          "razsocforn": null,
          "numcgccpf": null
        }
      ]
    }
  ]
};

var codmatFiltro = "0121000003";

var objFiltrado = obj.ordserv.filter(function(o) {
   return o.Equipamento.some(function(e){
     return e.codmat == codmatFiltro;
   });
});
   
console.log(objFiltrado);

Here the documentation of some: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

Browser other questions tagged

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