Proper way on Ionic to fetch an json?

Hi guys, i’ve a JSON who i retrieve if i do a http.get

{
"Town": {

"employ": 0,
"bussiness": 0,
"local": 0
},

"Village": {

"employ": 2,
"bussiness": 0,
"local": 0
},

"Country": {

"employ": 2,
"bussiness": 1,
"local": 0
},


"Land": {

"employ": 0,
"bussiness": 0,
"local": 0
},
}

and i can access to his elements if i do (for example) this.http.get(this.url).map(res => res.json().subscribe( data =>

and then i can acceed doing data.town.employ, but if i need to know how array got > 0 it’s a bad method (in my opinion) to fetch the data (and it isn’t scalable).

How can i find documentation to achieve this in the proper way?

What do you mean by that?

I need fetch the array to check if anyone got more than a cero

I still don’t understand.

You have a server that returns this JSON.
You want to know " if anyone got more than a cero".
Go through the object (this is not an array!) and check if the values are >0.

That’s ok… What’s the proper way to achieve that??

You mean how to iterate through an object ?
If yes:

for (let key in data.Town) {
  if (data.Town[key] > 0) {
    // do your stuff
  }
}

Yes, but i want to iterate ALL the object to check if there are any new objects

I think you can use this to get your keys in an array

And use this array to check the child elements.

Humm it could be possible, this is i want to check, if there are any values incoming

"NEWVALUE": {

"employ": 2,
"bussiness": 3,
"local": 0
},

and check if got > 0

You can just do a nested loop here:

for (let key in data) {
  for (let k in data[key]) {
    if (data[key][k] > 0) {
       // do your stuff
    }
  }
}

I think this is more efficient than using object.keys to get an array of keys and then loop through this array and loop again to the child elements since this will use 3 loops

1 Like

Do you mean "how can i get new records in a scalable way?"
You have to change your API so you can ask for new and modified records since a certain date.
get(‘myapi/employers?changedsince=2017-09-18’)
Then you need to add new records and update the existing records

This works, but after this how can i obtain the names of the keys? For example how can i see what are the name of the values who got > 0? For example: NEWVALUE HAVE 2 EMPLOY AND 3 BUSSINESS

Thanks!

From the example above key and k will give you the name of the current property.
To give you an example of how you can do it (not saying that this is the best solution don’t get me wrong this is just an example for your case)

for (let key in data) {
  let arr = [];
  for (let k in data[key]) {
    if (data[key][k] > 0) {
      arr.push(k + ' ' + data[key][k]);
    }
  }
  if (arr.length) {
    console.log(key + ' ' + 'HAVE' + ' ' + arr.join(' '));
  }
}

Hope this will give you an idea

IMHO this sort of task should be done on the server. The application server should be sending data that does not need tedious postprocessing to be done by the app, because the app is working with more limited resources.

1 Like