0

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);
}
Baelix
  • 261
  • 2
  • 7
  • 17
  • Because you've defined a property named `string` on the object. –  Jul 19 '13 at 17:13
  • 1
    Your `cityList` consists of `location` objects which have `string` properties. What else did you expect? – Bergi Jul 19 '13 at 17:13
  • Is there a way I can print this out without having the "string:" object printed with it? Is there another data type I should focus on? – Baelix Jul 19 '13 at 17:14
  • You could simply build the string, and push it into the `cityList` instead of creating an object. You'll need to be sure to update your `sortFunc` too –  Jul 19 '13 at 17:16
  • 1
    It's not a data type, it's just the property which *you* have choosen. – Bergi Jul 19 '13 at 17:17

2 Answers2

1

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

Community
  • 1
  • 1
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • You're correct; this is a group project. I wrote bit for reading from the doc, and interfacing with the web service. Partner wrote the parse. – Baelix Jul 19 '13 at 17:24
  • I believe (and hope) the second part of my answer is more helpful than the first, then. – bfavaretto Jul 19 '13 at 17:28
  • This is fixed now, but I thought I would just ask: when I entered `console.log(objectList[1].string)` into `printData` to try testing it, it comes up as undefined for my object. – Baelix Jul 19 '13 at 17:29
  • @Baelix And what's the output for `console.log(objectList)`, is it the same as in your question? – bfavaretto Jul 19 '13 at 17:36
  • No, it's `undefined` now. Not sure why, as the array is populated. Could it be that it's still trying to read the object property somehow? (in this case, since we're just ad hoc appending to the object, would it not have a property/type?) – Baelix Jul 19 '13 at 19:01
  • @Baelix Did you change anything in your code? The second part of my answer was assuming no changes to the code you posted (thus, no changes to the array). – bfavaretto Jul 19 '13 at 19:04
0

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.

Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
  • This is extremely helpful. I wasn't thinking about the fact that pushing another object into an array would log that object type. Thanks you! – Baelix Jul 19 '13 at 17:18