1

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

3
  • 3
    So you wish to filter campaigns or books in a given campaign? It looks like you have a campaigns array and a nested array of books, so are you trying to filter campaigns where there are no active books? or at least 1 active book? Commented Aug 15, 2019 at 14:22
  • I am trying to filter campaigns where there is atleast one Inactive book Commented Aug 15, 2019 at 14:26
  • o.Book.Active = false is an assignment, not an equality check, 'Book.Active' == false is always false, 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) Commented Aug 15, 2019 at 14:28

2 Answers 2

2

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>

Sign up to request clarification or add additional context in comments.

Comments

0

you have an object key called edges which is your array of nodes.

const results = edges.filter( edge => {
  return edge.node.Promoted_Titles.filter( promotedTitles => promotedTitles.Book.Active === false).length > 0)
});

This will return a list of edges which contain at least 1 inactive book.

Edit: I just swapped it around to show a list of edges / campaigns where there is at least 1 inactive book per your comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.