Skip to main content

Why do I keep getting "undefined" for each item in this JSON array?



The following is returning 'undefined' as the value for each item. Can someone show me why?





Jquery







$("button").click(function () {

var estimateID = this.id;

var baseURL = "/Dashboard/EstimateDetails/";

var url = baseURL + estimateID;

$.getJSON(url, function (data) {

for (var i = 0; i <= data.details.length; i++) {

$('#Details').html("<p>item1=" + data.details.Dma + " item2=" + data.details.Callsign + " item3=" + data.details.Description + "</p>");

}

});







JSON







{

"details": [{

"Dma": "Albany-Schenectady-Troy",

"CallSign": "WRGB",

"Description": "WRGB (CBS) Schenectady"

}, {

"Dma": "Albany-Schenectady-Troy",

"CallSign": "WTEN",

"Description": "WTEN (ABC) Albany "

}, {

"Dma": "Albany-Schenectady-Troy",

"CallSign": "WXXA",

"Description": "WXXA (Fox) Albany "

}, {

"Dma": "Atlanta",

"CallSign": "WGCL",

"Description": "WGCL (CBS) Atlanta "

}, {

"Dma": "Atlanta",

"CallSign": "WXIA",

"Description": "WXIA (NBC) Atlanta "

}, {

"Dma": "Austin",

"CallSign": "KXAN",

"Description": "KXAN (NBC) Austin "

}, {

"Dma": "Austin",

"CallSign": "KVUE",

"Description": "KVUE (ABC) Austin "

}, {

"Dma": "Baltimore",

"CallSign": "WMAR",

"Description": "WMAR (ABC) Baltimore "

}, {

"Dma": "Baltimore",

"CallSign": "WBAL",

"Description": "WBAL (NBC) Baltimore"

}, {

"Dma": "Baltimore",

"CallSign": "WJZ ",

"Description": "WJZ (CBS) Baltimore "

}, {

"Dma": "Baltimore",

"CallSign": "WBFF",

"Description": "WBFF (Fox) Baltimore "

}]

}







EDIT - the result when I run the above code is a single line of HTML added to the details div as follows:





item1=undefined item2=undefined item3=undefined


Comments

  1. data.details.Callsign is wrong

    data.details[i].Callsign

    is what you want, i.e. you are missing the array index as you loop over details. Notice you are doing that for multiple variables.

    Cleaner code might help. Separating the variables out like so is a good start...

    $.getJSON(url, function (data) {
    var details = data.details,
    dma, callSign,...; // and the rest of whatever you need

    for (var i = 0; i <= details.length; i++) {
    dma = details[i].Dma;
    callSign = details[i].Callsign;
    // more here

    $('#Details').html("<p>item1=" + dma + " item2=" + callSign + ...
    }
    });


    Is much more readable, and less error prone IMHO.

    EDIT -- as pointed out in comments, html will overwrite the content of your div each time in the loop. You probably want append instead.

    ReplyDelete
  2. You left off the array index in your loop.

    $("button").click(function () {
    var estimateID = this.id;
    var baseURL = "/Dashboard/EstimateDetails/";
    var url = baseURL + estimateID;
    $.getJSON(url, function (data) {
    for (var i = 0; i <= data.details.length; i++) {
    $('#Details').html("<p>item1=" + data.details[i].Dma + " item2=" + data.details[i].Callsign + " item3=" + data.details[i].Description + "</p>");
    }
    });

    ReplyDelete
  3. You've defined details as an array, therefore you need to index it to access its members. In the for loop, for example, you want this type of thing:

    data.details[i].Dma


    instead of

    data.details.Dma


    By the way, you could use a jQuery each callback instead of a for loop:

    if (data && data.details) {
    $.each(data.details, function() {
    $('#Details').html("<p>item1=" + this.Dma + " item2=" + this.Callsign +
    " item3=" + this.Description + "</p>");
    });
    }


    Within the callback function passed to each, this stands in for data.details[i] in the for loop.

    ReplyDelete
  4. Use

    data.details[i].Dma etc

    Also add -1 in the loop data.details.length-1, because index starts with 0.

    for (var i = 0; i <= data.details.length-1; i++) {
    $('#Details').html("<p>item1=" + data.details[i].Dma + " item2=" + data.details[i].Callsign + " item3=" + data.details[i].Description + "</p>");
    }


    Demo: http://jsfiddle.net/aqPvL/

    ReplyDelete
  5. inside the for loop you need to reference your objects via data.details[i]

    for example

    data.details[i].Callsign


    not

    data.details.Callsign

    ReplyDelete
  6. add the index to the end of data.details

    data.details.Dma becomes data.details[i].Dma

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex