0

I am trying to filter the projects that only contain "isFavorite": true.

temp1 = {
  "Name": "Toronto",
  "Area": [ 
    {
      "Title": "Roofing",
      "Details": {
        "Reports": 52,
        "Projects": [ 
          {
            "Name": "ITEM A",
            "isFavorite": true
          }, 
          {
            "Name": "ITEM B",
            "isFavorite": false
          }, 
          {
            "Name": "ITEM C",
            "isFavorite": true
          } 
        ]
      }
    } 
  ]
}

I tried using lodash filter function but the array returns everything.

_.filter(temp1, {Area: [{Projects: [{isFavorite: true}] }]});

I tried using regular javascript but still doesn't return only the projects that are "isFavorite": true

this.branchSummaries.filter(d => d.Area.some(p => p.Projects.some(f => f.isFavorite === true)));

1 Answer 1

2

you need first to loop through the Areas after that filter projects in each Area it would be like this

 let data =  {
      "Name": "Toronto",
      "Area": [
        {
          "Title": "Roofing",
          "Details": {
            "Reports": 52,
            "Projects": [
              {
                "Name": "ITEM A",
                "isFavorite": true
              },
              {
                "Name": "ITEM B",
                "isFavorite": false
              },
              {
                "Name": "ITEM C",
                "isFavorite": true
              }
            ]
          }
        }
      ]
    }

data.Area.forEach(area => {
  area.Details.Projects = area.Details.Projects.filter(project => project.isFavorite)
})
console.log(data)

Sign up to request clarification or add additional context in 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.