I'm having a lot of troubles to get the data from thingspeak into a json response. I have a url and the response of that give me a many fields. This is the response of the JSON:
?({"channel":{"id":XXXXX,"name":"XXXXX","field1":"Temperature","field2":"Humidity","created_at":"2015-11-03T13:12:06Z","updated_at":"2015-11-15T12:07:37Z","last_entry_id":142},"feeds":[{"created_at":"2015-11-14T21:06:16Z","entry_id":136,"field1":"0"},{"created_at":"2015-11-14T21:06:39Z","entry_id":137,"field1":"25"},{"created_at":"2015-11-14T21:06:59Z","entry_id":138,"field1":"24.05"},{"created_at":"2015-11-14T21:07:13Z","entry_id":139,"field1":"24.45"},{"created_at":"2015-11-14T21:08:16Z","entry_id":140,"field1":"24.45"},{"created_at":"2015-11-15T12:06:18Z","entry_id":141,"field1":"24.5"},{"created_at":"2015-11-15T12:07:37Z","entry_id":142,"field1":"21.4"}]})
I want to retrieve the maximum and minimum value of each field1 data. I have been reading and the response is not in int, therefore should be converted to an int array.
This is my code at the moment:
$.getJSON('http://api.thingspeak.com/channels/'+channel+'/field/1.json?callback=?',
{key: read_API_key, days: "1"},
function(data) {
$.each(data.feeds, function() {
var temp_vals = this.field1;
var temp_vals_date = this.created_at;
console.log(temp_vals);
});
}
);
I want to search inside feed array looking for the field1 numbers and save into an int array to after do the maths using Math.max.apply(Math, temp_vals); Using the console, the values are saved correctly into temp_vals, but I cannot use that function, with the following error: Function.prototype.apply: Arguments list has wrong type.
Therefore, how can I change the response into a int array or something to be able to find the max and min value? And another quick question, is any way to print the values outside of the $.each(data.feeds,function(){});, because I didn't find a way to do it....
Changing to parseInt:
$.getJSON('http://api.thingspeak.com/channels/'+channel+'/field/1.json?callback=?',
{key: read_API_key, days: "1"},
function(data) {
$.each(data.feeds, function() {
var temp_vals = parseInt(this.field1);
var temp_vals_date = this.created_at;
var temp_max = Math.max.apply(Math, temp_vals);
console.log(temp_max);
$('#temp1_max').text(temp_max + ' ºC');
console.log(temp_vals);
});
}
);