Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i filter by value Angular Array or Angular model?

I have this kind of data that I pulled from my service and I’m importing it into an angular model. What I want to do here is to find those whose boolresult value is true in the model. I used the find method for this, but there is only 1 element, I want to get all of them, how can I achieve this?

Example Array

[
    {
        "ownername": "Owner",
        "result": "Result1",
        "clientname": "ClientName1",
        "boolresult": true,
        
    },
    {
        "ownername": "Owner",
        "result": "Result2",
        "clientname": "ClienName2",
        "boolresult": true,
        
    },
    {
        "ownername": "Owner",
        "result": "Result3",
        "clientname": "ClienName3",
        "boolresult": false,
        
    },
    {
        "ownername": "Owner",
        "result": "Result4",
        "clientname": "ClienName4",
        "boolresult": false,
        
    }
]

Here is the code I tried to convert this result to a model and show only the true ones.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private getAllResults(){
    let apiEndpoint = "results"
    this.httpRequestService.getApi(apiEndpoint, false).subscribe(resultRequest => {
      
      this.serviceResults = resultRequest; // This is Array to MyModel
      this.serviceResults.sort((a, b) => new Date(b.createddate).getTime() - new Date(a.createddate).getTime());
      console.log(this.serviceResults)
      
      this.approvedResults = this.serviceResults.find(item => item.boolresult == true) // I Tried this
      console.log(this.approvedResults)
      
    })
    
  }

When I try this way, only 1 element is showing, I want to show all the values with boolresult true.How can I provide this?

>Solution :

user filter method rather than find method

find()=> The first element in the array that satisfies the provided testing function

filter()=>A shallow copy of a portion of the given array, filtered down to just the elements from the given array that pass the test implemented by the provided function

var serviceResults=[
    {
        "ownername": "Owner",
        "result": "Result1",
        "clientname": "ClientName1",
        "boolresult": true,
        
    },
    {
        "ownername": "Owner",
        "result": "Result2",
        "clientname": "ClienName2",
        "boolresult": true,
        
    },
    {
        "ownername": "Owner",
        "result": "Result3",
        "clientname": "ClienName3",
        "boolresult": false,
        
    },
    {
        "ownername": "Owner",
        "result": "Result4",
        "clientname": "ClienName4",
        "boolresult": false,
        
    }
]
var approvedResults = serviceResults.filter(item => item.boolresult == true) // I Tried this
console.log(approvedResults)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading