-3

I have a huge JSON file that I need to cull down based on some values of keys for nested objects. Basically the JSON file looks like this:

{
   "Card One": {
     "colors": ["G", "R"],
     "layout": "normal",
     "leadershipSkills": {"brawl": false, "commander": true},
     ///etc
   },
   "Card Two": {...},
   "Card Three": {...},
   /// etc
}

How can I filter through each key/value pair in this object for only the pairs where "commander": true is found?

Alexa Aria
  • 23
  • 4

1 Answers1

1

I provide a solution using Object.keys() and iterate it,waiting to see more elegant solution

let data =
{
   "Card One": {
     "colors": ["G", "R"],
     "layout": "normal",
     "leadershipSkills": {"brawl": false, "commander": true}
   },
      "Card Two": {
     "colors": ["G", "R"],
     "layout": "normal",
     "leadershipSkills": {"brawl": false, "commander": false}
   },
      "Card Three": {
     "colors": ["G", "R"],
     "layout": "normal",
     "leadershipSkills": {"brawl": false, "commander": false}
   }
}

let keys = Object.keys(data)
let result ={}
keys.forEach(k => {
  if(data[k].leadershipSkills.commander){
      result[k] = data[k]
   }
})

console.log(result)
flyingfox
  • 13,414
  • 3
  • 24
  • 39