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);
}
You're creating a location object:
var obj = new location(city, state, temp);
In that object, you create a string property:
this.string = city + ", " + state + " : " + currentTemp;
If what you want is a simple array of strings, change textParse to do this:
cityList.push(city + ", " + state + " : " + currentTemp);
(instead of pushing a location object)
This would require you to rewrite the sort function too.
It looks like you didn't write that code, otherwise you would understand it. Maybe what you're missing is that objectList is an array of objects. You can access your data by array index, then object property (in this case, string). For example, try this in printData:
console.log(objectList[1].string); // 'Houston, TX : 86'
For further info on how to traverse your data, see Access / process (nested) objects, arrays or JSON
It looks like you're pushing Javascript objects into the cityList array, so when you call printData(cityList), you're logging an array object, not a string. So, the console spits out JSON. If you want raw text, you need to build a string.