如何反转数组总和的顺序?

时间:2015-07-10 06:50:29

标签: jquery arrays

请参阅此Fiddle了解结果。我想颠倒FINAL专栏的顺序。代码片段:

success: function (e) {
        var t = e,
            finalAccum = []
            arr = [],
            rev = [];
        $.map(t, function (e) {
            var loc = e.location;
            var prov = e.province;
            var data = e.data;
            for (var i = 0; i < data.length; i++) {
                var dataEl = data[i];
                var len = parseInt((data.length - 1) - i);
                var accumRain = parseFloat(data[i].rain_value);
                finalAccum = parseFloat(accumRain + finalAccum);
                t = dataEl.dateTimeRead;
                $('#ul2').append("<li>" + t + "</li>");
                $('#ul1').append("<li>" + accumRain + "</li>");
                $('#ul').append("<li>" + finalAccum + "</li>");
            }
        });
    }

我对反向的意思是,例如结果是1,2,3,4,5应该是5,4,3,2,1。

2 个答案:

答案 0 :(得分:2)

简单,从

更改行

var dataEl = data[i]

var dataEl = data[data.length-i-1];

答案 1 :(得分:0)

您可以简单地反转for方法中的逻辑以向后迭代数组:

var array = $.map(t, function (i, e) {
    var loc = e.location;
    var prov = e.province;
    var data = e.data;            
    for (var i = data.length -1; i >= 0; i--) {
        var dataEl = data[i];
        var accumRain = parseFloat(dataEl.rain_value);
        finalAccum = parseFloat(accumRain + finalAccum);
        t = dataEl.dateTimeRead;
        $('#ul2').append("<li>" + t + "</li>");
        $('#ul1').append("<li>" + accumRain + "</li>");
        $('#ul').append("<li>" + finalAccum + "</li>");
    }
});

Example fiddle