vishnu
vishnu

Reputation: 4599

Filter the JSON data using angular 4 filters

I am fetching the content(data) from local JSON file using http in the following way,

 getIssuerTestCases(totalPage) { 
        // console.log(formdata);
        const searchurl = `assets/data/issuer_test_cases.json`;

        return this.http.get(searchurl, {}).map(
           result => {
               console.log(result.json());
               return result.json();
           },
        );
        // .filter(result => (result.content.initiated_by).sessionStorage.getItem('initiatedby') > -1);
      }

And i want to filter the JSON content based on the user role. if the user role is issuer, i want to fetch only the 'issuer' data. can any one help to filter the array.

 {

    "content": [
        {
            "testcase": "PREQ_001 - Payment request",
            "iterationcount": "COMFORT",
            "test_case_id": "PREQ_001",
            "initiated_by": ["issuer"]
        },
        {
            "testcase": "PREV_002 - Payment reversal",
            "iterationcount": "COMFORT",
            "test_case_id": "PREV_002",
            "initiated_by": ["issuer", "acquirer"]
        },
       {
            "testcase": "PREV_0 - Payment reversal",
            "iterationcount": "COMFORT",
            "test_case_id": "PREV_0",
            "initiated_by": ["CDF"]
        },

   ]
}

Upvotes: 0

Views: 404

Answers (3)

Krishna Rathore
Krishna Rathore

Reputation: 9687

You can try with this solution.

console.log(result.json());
let jsonData= result.json();
return jsonData.content.filter(data => data.initiated_by.indexOf("issuer") > -1)

Upvotes: 2

Himanshu
Himanshu

Reputation: 855

Use can check for index of issuer in initiated_by array and filter it if present.

Sample code:

return result.json().content.filter(item => ids.indexOf("issuer") !== -1);

Upvotes: 0

user7158437
user7158437

Reputation:

Plese Try this

 ngOnInit() {
    this.result= this.books.filter(
      book => book.store_id === this.store.id);
  }

In place of books but ur data

Upvotes: 0

Related Questions