这是输入json
set = {
"pending": [
{
"is_active": true,
"order_updated": false,
"po_id": "m86lu",
}, {
"is_active": true,
"order_updated": false,
"po_id": "m86lu",
}, {
"is_active": true,
"order_updated": false,
"po_id": "m86l89u",
}]}
set = set.pending[0].filter(({ po_id }) => {
return po_id === 'm86lu';
});
我只需要获取具有po_id'm86lu'的json集。
输出必须是这样
set = {
"pending": [
{
"is_active": true,
"order_updated": false,
"po_id": "m86lu",
}, {
"is_active": true,
"order_updated": false,
"po_id": "m86lu",
}
]}
如何获取?看来我在错误地使用filter
函数。
答案 0 :(得分:2)
挂起的密钥将以这种方式丢失
set = {
pending: set.pending.filter(({ po_id }) => { return po_id === 'm86lu'; })
}
答案 1 :(得分:0)
遍历json,并根据评论的建议将其一一添加到新集合中。
var input = {
"pending": [
{
"is_active": true,
"order_updated": false,
"po_id": "m86lu",
}, {
"is_active": true,
"order_updated": false,
"po_id": "m86lu",
}, {
"is_active": true,
"order_updated": false,
"po_id": "m86l89u",
}]}
var output = {};
for (var key in input) {
if (!input.hasOwnProperty(key)) {
continue;
}
let content = input[key].filter(({ po_id }) => { return po_id === 'm86lu'; });
output[key] = content;
}
console.log(output);