0

I want to filter a json array that looks like the below code:

const data = 
  [ { __typename  : 'GEP_validate_response_graph_type'
    , status      : 'APROVADO_COM_ACOES'
    , informative : [ 'Manifestação obrigatória sem documento.'] 
    , pendencias  : 
      [ { __typename : 'GEP_validate_action_graph_type'
        , action     : 'MANIFESTACAO_OBRIGATORIA_SEM_DOCUMENTO'
        , optional   : false
        , message    : 'Manifestação obrigatória sem documento.'
        , id         : null
      } ] 
    , tarefa_id   : 'e4cdb007-a223-5547-a49f-c47c06fd2c52'
    } 
  , { __typename  : 'GEP_validate_response_graph_type'
    , status      : 'APROVADO_COM_ACOES'
    , informative : [ 'Manifestação obrigatória sem documento.'] 
    , pendencias  : 
      [ { __typename : 'GEP_validate_action_graph_type'
        , action     : 'MANIFESTACAO_OBRIGATORIA_SEM_DOCUMENTO'
        , optional   : true
        , message    : 'Manifestação obrigatória sem documento.'
        , id         : null
      } ] 
    , tarefa_id   : '361e4c79-9605-fd4f-b7bd-47a3916ad070'
  } ] 

I want to return only the optional == true, and after that I will get the lenght of this object.

I tryed this, but did not work:

const pedido_filter = acoes_filter.pendencias.filter(
    (pendencia) =>
      pendencia.optional == true
  );

How do I do that?

2
  • Does "pendencias" always have one item\object? Commented Nov 11, 2021 at 4:26
  • I see no acoes_filter in your data. It's also a really bad idea to work with non-English property names. Commented Nov 11, 2021 at 4:43

1 Answer 1

1

You can use Array.some for testing if the pendencias array contains atleast one optional key with value as true

let x = [{
    "__typename": "GEP_validate_response_graph_type",
    "status": "APROVADO_COM_ACOES",
    "informative": [
      "Manifestação obrigatória sem documento."
    ],
    "pendencias": [{
      "__typename": "GEP_validate_action_graph_type",
      "action": "MANIFESTACAO_OBRIGATORIA_SEM_DOCUMENTO",
      "optional": false,
      "message": "Manifestação obrigatória sem documento.",
      "id": null
    }],
    "tarefa_id": "e4cdb007-a223-5547-a49f-c47c06fd2c52"
  },
  {
    "__typename": "GEP_validate_response_graph_type",
    "status": "APROVADO_COM_ACOES",
    "informative": [
      "Manifestação obrigatória sem documento."
    ],
    "pendencias": [{
      "__typename": "GEP_validate_action_graph_type",
      "action": "MANIFESTACAO_OBRIGATORIA_SEM_DOCUMENTO",
      "optional": true,
      "message": "Manifestação obrigatória sem documento.",
      "id": null
    }],
    "tarefa_id": "361e4c79-9605-fd4f-b7bd-47a3916ad070"
  }
];


x = x.filter((item) => item.pendencias.some(y => y.optional));
console.log(x);

Sign up to request clarification or add additional context in comments.

4 Comments

Logic is returning both objects, one with optional as false also
Sorry, updated my answer. I missed that part. @MohitKushwaha.
Just too add, there's also Array.every(), then again it depends on the question if pendencias really have more than 1 record and all must be optional == true.
That solved my problem, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.