I'm writing a program that takes in some data from a file (in this case, a list of IDs).
This program then takes the IDs, interfaces with a weather server, grabs the information the server kicks back, and parses it.
It then sorts the data in order of name by city, and pushes it into an array.
I'm trying to get it printed out, but when I print the array, I keep getting the following output:
[ { string: 'Dallas, TX : 91' },
{ string: 'Houston, TX : 86' },
{ string: 'Houston, TX : 86' },
{ string: 'Jacksonville, FL : 83' },
{ string: 'Laguna Hills, CA : 67' },
{ string: 'New York, NY : 91' },
{ string: 'Seattle, WA : 62' } ]
Naturally, I anticipate having the square brackets included, and the commas as well. However, why is it printing out the "string:" and curly braces?
Here is my source:
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var lineReader = require('line-reader');
var cityList = [];
var numItems = 0;
var needToPrint = 0;
lineReader.eachLine('IDList.txt', function(line, last) {
numItems++;
getResponse(line, textParse);
});
var getResponse = function(id, callback){
var request = new XMLHttpRequest;
request.open("GET", "http://weather.yahooapis.com/forecastrss?w=" + id +"&u=f");
request.onreadystatechange = function(){
if(request.readyState === 4 && request.status === 200){
var type = request.getResponseHeader("Content-Type");
if(type.indexOf("xml") !== -1 && request.responseXML)
callback(request.responseXML);
else if(type === "application/json")
callback(JSON.parse(request.responseText));
else
callback(request.responseText);
}
};
request.send(id);
}
var textParse = function (input)
{
var index = input.indexOf("city=\"") + "city=\"".length;
var endIndex = input.indexOf("\" region=\"");
var city = input.substring(index, endIndex);
index = input.indexOf("region=\"") + "region=\"".length;
var state = input.substring(index, index + 2);
index = input.indexOf("temp=\"") + "temp=\"".length;
endIndex = input.indexOf("\"", index);
var temp = input.substring(index, endIndex);
var obj = new location(city, state, temp);
cityList.push(obj);
cityList.sort(sortFunc);
needToPrint++;
if(numItems === needToPrint)
printData(cityList);
}
var location = function (city, state, currentTemp)
{
this.string = city + ", " + state + " : " + currentTemp;
};
var sortFunc = function(input1, input2)
{
if (input1.string < input2.string) //sort string ascending
return -1
if (input1.string > input2.string)
return 1
return 0 //default return value (no sorting)
}
var printData = function(objectList){
console.log(objectList);
}