[英]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.