【问题标题】:Jquery object is undefined after sort but is in firebug排序后 Jquery 对象未定义,但在 firebug 中
【发布时间】:2015-08-24 13:50:20
【问题描述】:

我对返回数据的脚本进行了 ajax 调用。我在萤火虫中看到了数据,它是正确的。当数据回来时,我会进行排序,它也可以工作。我在萤火虫中看到对象按我想要的方式排序。排序后,我尝试访问键/值对。我在萤火虫中看到我想要的键/值在对象中。当我尝试使用对象键/值分配给 var 时,它是未定义的。我正在尝试汇总重复数据。如果我没有使用最好的方法,请帮助我使用更好的方法。

数据样本

coreDate "060115" coreType "asterisk_11" fileName "/var/www/html/Cores/gdb.hamid.asterisk.060115" jid "hamid"

代码

    $.when (
            $.ajax( {
                    type:"GET",
                    url: 'cf.php',
                    dataType: 'json',
                    timeout: 120000,
            })
    )
    .done(function(coreInfo) {
            coreInfo.sort(function(a,b) { return a.coreType > b.coreType } );
            var coreStats={};
            coreStats.numOfCores=0;
            coreStats.numOfCoreType=0;
            coreStats.prevCoreType=coreInfo.coreType;
// for some reason coreInfo.coreType is not defined.  The coreInfo object exists and coreType is there.  I tried this.coreType but is undefined.
            $.each(coreInfo, function() {
                    coreStats.numOfCores++;
                    if (coreStats.prevCoreType != this.coreType) {
// This works.  why do I have to access this.coreType instead of coreInfo.coreType?
                            $('#list > tbody:last-child').append('<tr><td><a href="'+this.fileName+'">'+this.coreType+'</td><td>'+coreStats.numOfCoreType+'<br>core</td><td>jid</td></tr>');
                            coreStats.numOfCoreType=0;
                    }
                    coreStats.numOfCoreType++;
                    coreStats.prevCoreType=this.coreType;
// setting prevCoreType works here
            });
            $('#cores').html("<h1>"+coreStats.numOfCores+"</h1><br>Core Files");
    })
    .fail(function() {
    });

【问题讨论】:

    标签: javascript jquery ajax object


    【解决方案1】:

    根据您的代码和描述,听起来coreInfo 是一个对象数组,对吗?如果它是一个对象,它就没有sort() 方法,你的代码就会停在那里。

    如果它是一个数组,它没有像coreInfo.coreType 这样的属性。数组的每个 元素 都是一个具有coreType 和其他属性的对象。这就是为什么您可以在 $.each() 回调中访问 this.coreType 的原因。 $.each() 逐个元素地遍历数组,并将每个单独的元素传递到回调中。

    您在 Firebug 中在 $.each() 之外看到的 coreInfo.coreType,我怀疑您实际看到的是 coreInfo[0].coreType,即数组的第一个元素。

    为了帮助跟踪变量是否为数组,我强烈建议给数组命名以反映这一事实:可以是复数词,或者在名称中添加ListArray,或其他。像coreInfo 这样的名字听起来像是一个单一的项目。我将数组命名为coreListcoreArray,或者简称为cores。我个人喜欢简洁的复数词:cores 表示核心信息数组,core 表示该数组中的单个元素。

    这样,当您看到 cores.coreTypecoreList.coreTypecoreArray.coreType 时,它会看起来错误。

    顺便说一句,对于更易读的代码,我建议不要在 $.each() 回调中使用 this。而是使用命名参数。

    $.each( coreInfo, function( i, core ) {
        coreStats.numOfCores++;
        if( coreStats.prevCoreType != core.coreType ) {
            $('#list > tbody:last-child').append(
                '<tr>' +
                    '<td>' +
                        '<a href="' + core.fileName + '">' +
                            core.coreType +
                        '</a>' +  // MISSING IN ORIGINAL
                    '</td>' +
                    '<td>' +
                        coreStats.numOfCoreType +
                        '<br>core' +
                    '</td>' +
                    '<td>' +
                        'jid' +
                    '</td>' +
                '</tr>'
            );
            coreStats.numOfCoreType = 0;
        }
        coreStats.numOfCoreType++;
        coreStats.prevCoreType = this.coreType;
    });
    

    请注意,上面代码中的标记中缺少&lt;/a&gt;。如果您以与可能缩进原始 HTML 代码相同的方式缩进标记字符串,而不是将其全部塞到一行中,则更容易发现此类错误。

    一个小问题:您不需要将$.ajax() 包裹在$.when() 中。 $.ajax() 返回一个可以直接使用的 Promise。

    您不需要计算coreStats.numOfCores 的代码。您可以简单地使用数组的length 属性。如果您按照我的建议调用数组cores,您可以这样做:

    $('#cores').html( "<h1>" + cores.length + "</h1><br>Core Files" );
    

    最后,您的sort 回调函数有一个错误,无法正确排序数组。它可能在一个简单的测试用例中有效,但它无法对其他数组进行排序。它必须处理所有三种情况:&lt;==&gt;,并为每种情况返回一个适当的值,可能像这样:

    coreInfo.sort( function( a, b ) {
        return(
            a.coreType < b.coreType ? -1 :
            a.coreType > b.coreType ? 1 :
            0
        );
    });
    

    为了验证我的假设,您能否发布一个实际 JSON 数据的示例,而不是格式化的表格?

    【讨论】:

    • 这里是从 script[{"coreDate":"050415","jid":"dspillman","coreType":"runBrowser_11","fileName":"\/var\/www\/html\/rhcCores \/coreFiles\/050415\/dspillman\/gdb.log.runBrowser_11.20150504.dspillman_CORE_040515-1202"},{"coreDate" :"050415","jid" 返回的原始未格式化 json: "ssamari","coreType":"mdMgr_10","fileName":"\/var\/www\/html\/rhcCores\/coreFiles\/050415 \/ssamari\/gdb.log.mdMgr_10.20150504.ssamari_CORE_040515- 1208"},
    • 是的,从开头的[ 可以看出,这是我猜测的对象的数组
    • coreInfo 是返回的 json.data。我在想它是一个对象。如果它是一个数组,那是默认行为还是我搞砸了?我会听取你对名字的建议。你让我找到了失踪的。这是一个doh!片刻。最初我按照您建议的调试方式格式化了代码。在我“认为”我做对了之后,我压缩了它,但仍然错过了。我也会更新排序功能。您的评论对我发送正确的路径非常有帮助。我将尝试使用数组索引访问列表中的第一个 coreType,看看是否可行。
    • coreInfo 是一个数组,一切都按预期工作,除了尝试访问整个数组而不是单个数组元素上的 coreType 和其他属性之外,您没有搞砸任何事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    相关资源
    最近更新 更多