Below is the sample array of objects.
I'm looking to filter this on basis of criteriaType, id & source. If none of the input.source match, the parent object should be filtered out. Also all the filter criteria are optional.
[{
"id": "9be6c6299cca48f597fe71bc99c37b2f",
"caption": "caption1",
"criteriaType": "type2",
"input": [
{
"id_1": "66be4486ffd3431eb60e6ea6326158fe",
"criteriaId": "9be6c6299cca48f597fe71bc99c37b2f",
"source": "type1",
},
{
"id_1": "1ecdf410b3314865be2b52ca9b4c8539",
"criteriaId": "9be6c6299cca48f597fe71bc99c37b2f",
"source": "type2",
}
]
},
{
"id": "b83b3f081a7b45e087183740b12faf3a",
"caption": "caption1",
"criteriaType": "type1",
"input": [
{
"id_1": "f46da7ffa859425e922bdbb701cfcf88",
"criteriaId": "b83b3f081a7b45e087183740b12faf3a",
"source": "type3",
},
{
"id_1": "abb87219db254d108a1e0f774f88dfb6",
"criteriaId": "b83b3f081a7b45e087183740b12faf3a",
"source": "type1",
}
]
},
{
"id": "fe5b071a2d8a4a9da61bbd81b9271e31",
"caption": "caption1",
"criteriaType": "type1",
"input": [
{
"id_1": "7ea1b85e4dbc44e8b37d1110b565a081",
"criteriaId": "fe5b071a2d8a4a9da61bbd81b9271e31",
"source": "type3",
},
{
"id_1": "c5f943b61f674265b8237bb560cbed03",
"criteriaId": "fe5b071a2d8a4a9da61bbd81b9271e31",
"source": "type3",
}
]
}]
I was able to achieve just filter by criteriaType & id. But I'm not able to filter by source also to make sure that parent isn't returned if none of the input.source match.
var json = <<array of objects>> ;
const {objectId: id, ctype: criteriaType, inputSource: source } = param; // getting the the params
json = ctype ? json.filter(({criteriaType}) => criteriaType === ctype ): json;
json = (objectId ? json.filter(({id}) => id === objectId ): json)
.map (({id, caption, criteriaType, input }) => {
//some manipulation
return { //results after manipulation}
})
Help me out! Thanks in advance. I'm not sure if we could chain filters to achieve it.
looking for esLint compatible code