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?
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.
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
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
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(' '));
}
}
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.