Reputation: 49
I am trying to write a short script to get some data. The idea is, instead of writing a lot of JavaScript files and I always need to hardcode the ID in the link I thought it must be possible to insert a variable into the link. Now I have the problem I only see the last value of my var as it looks like the var will be overwritten all time instead of simply adding the ID to the output. I hope you can help me.
Here is the code:
myConnector.getData = function(table, doneCallback) {
var date = new Date();
edate = date.toISOString().slice(0,10) + "-23-59-59";
var ids = ["3775", "6697"]
for ( i = 1; i < ids.length; i++)
var id_temp = ids[i];
{
$.getJSON("https://****/api/historicdata.json?id="+id_temp+"&avg=0&sdate=2016-12-12-00-00-00&edate="+edate+"&username=***&passhash=****", function(resp) {
var feat = resp.histdata,
tableData = [];
// Iterate over the JSON object
//String id = UUID.randomUUID().toString();
for (var j = 0, len = feat.length; j < len; j++) {
tableData.push({
//"no" : j,
"ids" : id_temp,
"datetime": feat[j].datetime,
"value": feat[j].value,
"value_raw": feat[j].value_raw,
"coverage": feat[j].coverage,
"coverage_raw": feat[j].coverage_raw
});
}
table.appendRows(tableData);
});
};
doneCallback();
};
Upvotes: 0
Views: 208
Reputation: 3319
its the issue that your entire code is not inside for loop , your for loop only have one line
use
var ids = ["3775", "6697"]
for ( i = 1; i < ids.length; i++)
{
var id_temp = ids[i];
$.getJSON("https://****/api/historicdata.json?id="+id_temp+"&avg=0&sdate=2016-12-12-00-00-00&edate="+edate+"&username=***&passhash=****", function(resp) {
Upvotes: 1
Reputation: 2187
The problem is that id_temp
only gets set with the last value because {
is in the wrong place.
This:
var ids = ["3775", "6697"]
for ( i = 1; i < ids.length; i++)
var id_temp = ids[i];
{
Should be:
var ids = ["3775", "6697"]
for ( i = 1; i < ids.length; i++)
{
var id_temp = ids[i];
//... rest of loop ...
Upvotes: 2