How to filter json return value using multiple form field value?

Dear Friends,

My doubt is,how to filter the json data using multiple field values my json return is : https://api.myjson.com/bins/x9onh

i need to filter it with 3 fields : intWardYearID, intWardNo, intDoorNO

i get the form field value for above 3 fields easily as in code:

getBuilding() {
    //alert('wardyr'+this.data.wardyear); --> Ward year
    // like this got all fields here!
    this.propertyProvider.getBuilding()
    .then(data => {
      this.data = data;
      alert(this.data);
      //console.log(this.users);
    });
  }

Please advise

Thanks

Anes

newArray = array.filter(item => property1 && property2 && property3)

But aaron,
I got error in typescript as

[ts] Property ‘filter’ does not exist on type ‘{}’.
any

my data type is

data:any = {};

Thanks

Get in the habit of typing your variables. If you do, you will have access to more powerful functions.

please advise how to make this

data: any = {}

type proper. I am new to ionic and angular js

It depends on what datatype this.propertyProvider.getBuilding returns. If it returns an Array, then you could write:

.then((data: Array) => {

Hi AndrW,

When I try to cast it to array got Type script as below:

My return value of that data is json

Can you post this code: this.propertyProvider.getBuilding?

sure

getBuilding() { //Search result for panchayat 
    return new Promise(resolve => {
      this.http.get(this.searchUrl).subscribe(data => {
        alert('from provider'+data);
        resolve(data);
      }, err => {
        console.log(err);
      });
    });
  }

my searchurl is : https://api.myjson.com/bins/x9onh

Don’t do this. It’s the explicit Promise construction antipattern. I don’t know where you are getting your code ideas from, but whichever tutorials you are reading, they are not good.

Then please advise a proper method to do my work sir …

You could try to filter with:

let result = JSON.parse(data).filter(function (entry) {
        return entry.intWardYearID==='someValue' && entry.intWardNo==='someValue && entry.intDoorNO==='someValue'
    });

I recommend you take a step back and read some documentation. The official Angular docs about HttpClient would be a good place to start.

Sir please provide that link which is helpful for me. I have a not mandatory field ‘Subnumber’ which may be given or left blank in search… How to handle that field in this ‘result’ filtration.

please advise

Well, if that filter is working, then it’s just a matter of coding from there…

I got that snippet from here: https://stackoverflow.com/questions/23720988/how-to-filter-json-data-in-javascript-or-jquery

yes that code works fine. Thanks alot AndreW :star_struck:

Hi Anespa, can you share your code to filter using multiple fields? Thanks