I have following data and unable to filter
{
"data": {
"Viewer": {
"CampaignGrids": {
"edges": [
{
"node": {
"Name": "mega campaign",
"Start_Date": "08/31/2020",
"End_Date": "09/15/2020",
"Promoted_Titles": [
{
"Primary_ISBN": "4314323211",
"Book": null
},
{
"Primary_ISBN": "94232344",
"Book": null
},
{
"Primary_ISBN": "221235345",
"Book": null
}
]
}
},
{
"node": {
"Name": "InActiveBook Campaign",
"Start_Date": "08/14/2019",
"End_Date": "08/14/2019",
"Promoted_Titles": [
{
"Primary_ISBN": "9781504011815",
"Book": {
"Primary_ISBN": "9781504011815",
"Active": true
}
},
{
"Primary_ISBN": "9780795336874",
"Book": {
"Primary_ISBN": "9780795336874",
"Active": true
}
},
{
"Primary_ISBN": "9781453244517",
"Book": {
"Primary_ISBN": "9781453244517",
"Active": true
}
},
{
"Primary_ISBN": "9781781892527",
"Book": {
"Primary_ISBN": "9781781892527",
"Active": false
}
}
]
}
}
]
}
}
}
}
I need to filter all the campaigns which contain Active : False book
So based on above data, i need to get back following
{"node": {
"Name": "InActiveBook Campaign",
"Start_Date": "08/14/2019",
"End_Date": "08/14/2019",
"Promoted_Titles": [
{
"Primary_ISBN": "9781504011815",
"Book": {
"Primary_ISBN": "9781504011815",
"Active": true
}
},
{
"Primary_ISBN": "9780795336874",
"Book": {
"Primary_ISBN": "9780795336874",
"Active": true
}
},
{
"Primary_ISBN": "9781453244517",
"Book": {
"Primary_ISBN": "9781453244517",
"Active": true
}
},
{
"Primary_ISBN": "9781781892527",
"Book": {
"Primary_ISBN": "9781781892527",
"Active": false
}
}
]
}
}
Currently i am using lodash library filter options. I tried following but it returns me both the data instead of just one
let flaged = _.filter(campaigns, function(item) {
return _.filter(item.Promoted_Titles, function(o) {
return o.Book.Active = false;
})
});
console.log('flagged campaign ', JSON.stringify(flaged) , '\n');
I also tried following
let flaged = filter(campaigns, function(el) {
el.Promoted_Titles = filter(el.Promoted_Titles, function(item) {
console.log('item is ', item);
return 'Book.Active' == false
})
return el;
})
But in both scenario, i get both item instead of just one. Thanks in advance
You can do this with Lodash by defining the structure of a match in a callback object like so:
let d = {"data":{"Viewer":{"CampaignGrids":{"edges":[{"node":{"Name":"mega campaign","Start_Date":"08/31/2020","End_Date":"09/15/2020","Promoted_Titles":[{"Primary_ISBN":"4314323211","Book":null},{"Primary_ISBN":"94232344","Book":null},{"Primary_ISBN":"221235345","Book":null}]}},{"node":{"Name":"InActiveBook Campaign","Start_Date":"08/14/2019","End_Date":"08/14/2019","Promoted_Titles":[{"Primary_ISBN":"9781504011815","Book":{"Primary_ISBN":"9781504011815","Active":true}},{"Primary_ISBN":"9780795336874","Book":{"Primary_ISBN":"9780795336874","Active":true}},{"Primary_ISBN":"9781453244517","Book":{"Primary_ISBN":"9781453244517","Active":true}},{"Primary_ISBN":"9781781892527","Book":{"Primary_ISBN":"9781781892527","Active":false}}]}}]}}}}
let e = d.data.Viewer.CampaignGrids.edges;
let a = _.filter(e, { node: { Promoted_Titles: [{ Book: { Active: false } }] } });
console.log(a);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>