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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.