javascript - 过滤带有值数组的 json - 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
数组是否包含至少一个optional
键,其值为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);
推荐阅读
- angular - Multi step Angular Form - Updating ngModel in hidden steps
- excel - 如何在 Excel 中的一行中应用数据验证工具
- jasper-reports - How can I get the first record of the table by using the same data-source for the main report and table in Jasper reports?
- sql - 通过在 where 子句中传递一个值来选择数据,如果值匹配则返回该数据,否则返回所有数据
- c++ - 为什么dynamic_cast从向量中的项目返回null?
- java - H2O AI:不支持的 MOJO 模型“word2vec”
- laravel - Vue根据电话号码输入和自动填写地址搜索客户详细信息
- python-3.x - 将值分配给数组中的列表
- docker - 如何减少在 Github Actions 上初始化容器的时间成本?
- matlab - 何时使用 PCA 进行降维?