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
o.Book.Active = falseis an assignment, not an equality check,'Book.Active' == falseis alwaysfalse, and nesting two_.filter()calls will always cause the outer callback to return a truthy value (which is effectively a shallow copy of the outer array)