【问题标题】:Why this long return statement is not working in Javascript?为什么这个长返回语句在 Javascript 中不起作用?
【发布时间】:2013-08-19 00:03:44
【问题描述】:

我有一个具有下一个结构的 JSON 对象:

{
    "matters": [
        {
            "title": "Systems",
            "date": "23/08/2010",
            "score": 5
        },
        ....
    ]
}

我想使用sort() 函数对这些数据进行排序。我可以使用score 字段来做到这一点,但我不能使用date 字段对其进行排序。这是我目前正在使用的:

$.getJSON('js/data.json', function(data) {
    // data now contains one node with all the matters
    $.each(data, function(key, val) {
        // val now contains one matter per nodes
        val.sort(function (a,b) {
            return
            parseInt(a.date.substring(6,10)+a.date.substring(3,5)+a.date.substring(0,2)) -
            parseInt(b.date.substring(6,10)+b.date.substring(3,5)+b.date.substring(0,2));
        });
        // Here I get the same array not sorted!
    }
});

parseInt() 两个函数都返回一个具有这种格式的整数:

if date=="23/08/2010" => 20100823

我使用警报来检查我是否正确拆分了日期,这很好。无论如何,我无法对数组进行排序。

我正在使用this JSON 文件测试代码。

我做错了什么?

【问题讨论】:

  • 你能发布一个两个日期排序错误的例子吗?还是数组根本没有排序?
  • 为我工作:jsfiddle.net/nEPTt
  • @CrazyTrain 你是对的。它在那里工作得很好。我怎么可能在我的 localhost 上拥有完全相同的代码并且它不起作用??!
  • 不确定。您确定代码完全相同吗? FWIW,我会做+a.date.split("/").reverse().join("") 来处理日期。干净一点。我也可能将处理后的日期存储在对象上,因此不需要继续处理相同的日期。 jsfiddle.net/Pcanx
  • 唯一的区别是他没有用 $.getJSON 加载 JSON 文件。您是否尝试过在函数中执行 console.log(data) 以查看您是否真的获得了 JSON 对象?

标签: javascript json sorting compare


【解决方案1】:

这是我如何实现排序的示例。我没有使用您的 JSON 文件,但您应该可以理解:

var data = {
    "matters": [{
        "title": "Sistema de Procesamiento de Datos",
            "date": "03/08/2011",
            "score": 8
    }, {
        "title": "Programación I",
            "date": "30/07/2010",
            "score": 7
    }, {
        "title": "Elementos de Investigación Operativa",
            "date": "07/08/2003",
            "score": 10
    }, {
        "title": "Programación III",
            "date": "05/08/2009",
            "score": 10
    }, {
        "title": "Laboratorio de Computación III",
            "date": "05/08/2010",
            "score": 10
    }]
};

$.each(data, function(key, val) {

    val.sort(function (a, b) {
        function date_to_int(d) {
            var parts = d.split('/');
            var day = parts[0];
            var month = parts[1];
            var year = parts[2];
            day = (day.length < 2) ? '0' + day : day;
            month = (month.length < 2) ? '0' + month : month;
            year = (year.length < 3) ? '20' + year : year;
            return year + month + day;
        }
        return date_to_int(a.date) - date_to_int(b.date);
    });
});

console.log(data);

JSFiddle 可以在这里找到:http://jsfiddle.net/bjarkehs/TJNE6/2/

【讨论】:

  • 对不起,我看错了结果。但最终你并没有做任何与 OP 大不相同的事情。你刚刚重构了代码。
  • 确实如此。然而,我无法从他所写的内容中解读出他的问题。我想也许他在分类执行时遇到了一些问题。我看到了您关于您建议如何处理比较的评论,我非常喜欢这个想法。我认为问题可能只是加载 JSON 文件。
【解决方案2】:

如果你仔细看看我的代码,你会注意到return 有一个断线,then 都是parseInt() 函数。

有错误。该函数没有返回正确的值。解决方案是删除所有的断线并将所有return 语句保留在一行中。

错误代码:

return
parseInt(a.date.substring(6,10)+a.date.substring(3,5)+a.date.substring(0,2)) -
parseInt(b.date.substring(6,10)+b.date.substring(3,5)+b.date.substring(0,2));

代码工作:

return parseInt(a.date.substring(6,10)+a.date.substring(3,5)+a.date.substring(0,2)) - parseInt(b.date.substring(6,10)+b.date.substring(3,5)+b.date.substring(0,2));

【讨论】:

    猜你喜欢
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    相关资源
    最近更新 更多