簡體   English   中英

使用值數組過濾 json - JavaScript

[英]Filter a json with array of values - JavaScript

我想過濾一個類似於以下代碼的 json 數組:

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'
  } ] 

我只想返回可選的 == true ,然后我會得到這個對象的長度。

我試過這個,但沒有奏效:

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

我怎么做?

您可以使用Array.some來測試pendencias數組是否包含至少一個值為true optional

 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM