i'm writing program takes in data file (in case, list of ids). program takes ids, interfaces weather server, grabs information server kicks back, , parses it.
sorts data in order of name city, , pushes array. i'm trying printed out, when print array, keep getting 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, anticipate having square brackets included, , commas well. however, why printing out "string:" , curly braces?
here 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 location object:
var obj = new location(city, state, temp); in object, create string property:
this.string = city + ", " + state + " : " + currenttemp; if want simple array of strings, change textparse this:
citylist.push(city + ", " + state + " : " + currenttemp); (instead of pushing location object)
this require rewrite sort function too.
it looks didn't write code, otherwise understand it. maybe you're missing objectlist array of objects. can access data array index, object property (in case, string). example, try in printdata:
console.log(objectlist[1].string); // 'houston, tx : 86' for further info on how traverse data, see access / process (nested) objects, arrays or json
Comments
Post a Comment