anespa
January 22, 2018, 4:46am
1
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)
anespa
January 22, 2018, 4:57am
3
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.
anespa
January 22, 2018, 5:00am
5
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) => {
anespa
January 22, 2018, 5:10am
7
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?
anespa
January 22, 2018, 5:12am
9
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.
anespa
January 22, 2018, 5:20am
11
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.
anespa
January 22, 2018, 5:29am
14
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
1 Like
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
anespa
January 22, 2018, 6:41am
16
yes that code works fine. Thanks alot AndreW
1 Like
Hi Anespa, can you share your code to filter using multiple fields? Thanks