我有下面的JSON数据集
[
{
"campaignId": 111,
"campaignCategory": "Diabetes",
"result": [
{
"campaignType": 1,
"name": "tes1"
},
{
"campaignType": 1,
"name": "test22"
},
{
"campaignType": 3,
"name": "test33"
}
]
},
{
"campaignId": 222,
"campaignCategory": "Orthopedic",
"result": [
{
"campaignType": 1,
"name": "Orthopedic"
}
]
},
{
"campaignId": 333,
"campaignCategory": "Cardiology",
"result": [
{
"campaignType": 3,
"name": "Cardiology"
},
{
"campaignType": 1,
"name": "Cardiology 123"
}
]
}
]
我对过滤器感到厌倦,但是没有返回所需的数据。
_.filter(summary, function (data) {
return (post, _.filter(data.result, {'campaignType': 3}));
应用过滤后,我想获得以下数据。
[{ campaignId: 111, campaignCategory: 'Diabetes', result: [{
campaignType: 3, name: 'test33'
}] },
{ campaignId: 333, campaignCategory: 'Cardiology', result: [{
campaignType: 3, name: 'Cardiology'
}] } ];
此处显示的所有节点都具有campaignType: 3
。
Lodash或基于纯Java脚本的解决方案都可以使用。
答案 0 :(得分:2)
您可以使用reduce
查找具有.result
为3的campaignType
项的对象,如果找到,则将它们推入累加器数组:
const getOutput = () => {
const output = arr.reduce((a, item) => {
const foundResults = item.result.filter(({ campaignType }) => campaignType === 3);
if (foundResults.length) {
a.push({ ...item, result: foundResults });
}
return a;
}, []);
console.log(output);
};
const arr = [{
campaignId: 111,
campaignCategory: 'Diabetes',
result: [{
campaignType: 1,
name: 'tes1'
}, {
campaignType: 1,
name: 'test22'
}, {
campaignType: 3,
name: 'test33'
}]
},
{
campaignId: 222,
campaignCategory: 'Orthopedic',
result: [{
campaignType: 1,
name: 'Orthopedic'
}]
},
{
campaignId: 333,
campaignCategory: 'Cardiology',
result: [{
campaignType: 3,
name: 'Cardiology'
},
{
campaignType: 1,
name: 'Cardiology 123'
}
]
}
];
getOutput();
答案 1 :(得分:1)
您可以在香草javascript中将filter
与some
和map
结合使用。
var data = [{ campaignId: 111, campaignCategory: 'Diabetes', result: [{
campaignType: 1, name: 'tes1'
},{
campaignType: 1, name: 'test22'
},{
campaignType: 3, name: 'test33'
}] },
{ campaignId: 222, campaignCategory: 'Orthopedic', result: [{
campaignType: 1, name: 'Orthopedic'
}] },
{ campaignId: 333, campaignCategory: 'Cardiology', result: [{
campaignType: 3, name: 'Cardiology'
},
{
campaignType: 1, name: 'Cardiology 123'
}]} ];
var res = data.filter(campaign => campaign.result.some(type => type.campaignType === 3));
res = res.map(campaign => {
campaign.result = campaign.result.filter(type => type.campaignType ===3);
return campaign;
});
console.log(res);
答案 2 :(得分:1)
您可以预先过滤内部result
并在存在ayn过滤器元素的情况下获取外部对象。然后以新结果获取一个新对象。
var data = [{ campaignId: 111, campaignCategory: 'Diabetes', result: [{ campaignType: 1, name: 'tes1' }, { campaignType: 1, name: 'test22' }, { campaignType: 3, name: 'test33' }] }, { campaignId: 222, campaignCategory: 'Orthopedic', result: [{ campaignType: 1, name: 'Orthopedic' }] }, { campaignId: 333, campaignCategory: 'Cardiology', result: [{ campaignType: 3, name: 'Cardiology' }, { campaignType: 1, name: 'Cardiology 123' }] }],
result = data.reduce((r, o) => {
var result = o.result.filter(({ campaignType}) => campaignType === 1);
if (result.length) r.push(Object.assign({}, o, { result }));
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 3 :(得分:0)