JS对象排序日期排序 [英] JS object sorting date sorting

查看:129
本文介绍了JS对象排序日期排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下定义的JS对象-

var item = {};
item[guid()] =
{
    symbol: $('#qteSymb').text(),
    note: $('#newnote').val(),
    date: $.datepicker.formatDate('mm/dd/yy', dt) + " " + dt.getHours() + ":" + minutes,
    pagename: getPageName()
};

在我的应用程序的某个时刻,我从chrome.storage中获得了这些列表(Items),我希望能够根据date

对其进行排序

这是我在做什么

var sortable = [];

            $.each(Items, function (key, value) {
                if (value.symbol == $('#qteSymb').text() || all) {                        
                    sortable.push([key, value]);
                }
            });

            console.log(sortable);

            sortable.sort(function (a, b) {
                a = new Date(a[1].date);
                b = new Date(b[1].date);
                return a > b ? -1 : a < b ? 1 : 0;
            });

            console.log(sortable);

它似乎不起作用.第一个和第二个console.log(sortable);是相同的.我尝试将return a > b ? -1 : a < b ? 1 : 0;更改为return a < b ? -1 : a > b ? 1 : 0;只是为了查看是否对sortable进行了任何更改,但是什么也没有发生... 谢谢〜

解决方案

两个console.log都显示相同的数组,因为当您使用console.log(sortable)时,sortable被引用传递,并且控制台输出在完成脚本后发生- sortable已经排序时.

使代码简单:

var arr = [3,2,1];
console.log(arr); // Produces `[1,2,3]` because its printed
                  // to the console after `arr.sort();`
arr.sort();
console.log(arr); // Produces `[1,2,3]`, as expected

演示: http://jsfiddle.net/Rfwph/


解决方法

如果希望对数组进行console.log修改后才能看到它,则可以使用.slice(0) 复制数组,即获取另一个包含与数组相同元素的数组.

var arr = [3,2,1];
console.log(arr.slice(0)); // [3,2,1]
arr.sort();
console.log(arr); // [1,2,3]

演示: http://jsfiddle.net/Rfwph/2/

最好使用.slice(0)代替.slice(),这在FF上受支持,但是ecma262规范说只有end参数是可选的.

I have JS object defined as follows -

var item = {};
item[guid()] =
{
    symbol: $('#qteSymb').text(),
    note: $('#newnote').val(),
    date: $.datepicker.formatDate('mm/dd/yy', dt) + " " + dt.getHours() + ":" + minutes,
    pagename: getPageName()
};

At some point in my app I am getting a list of those (Items) back from chrome.storage and I would like to be able to sort it based on the date

Here is what I am doing

var sortable = [];

            $.each(Items, function (key, value) {
                if (value.symbol == $('#qteSymb').text() || all) {                        
                    sortable.push([key, value]);
                }
            });

            console.log(sortable);

            sortable.sort(function (a, b) {
                a = new Date(a[1].date);
                b = new Date(b[1].date);
                return a > b ? -1 : a < b ? 1 : 0;
            });

            console.log(sortable);

It doesn't seem to work. The first and second console.log(sortable); is the same. I have tried changing return a > b ? -1 : a < b ? 1 : 0; to return a < b ? -1 : a > b ? 1 : 0; just to see if I am getting any change to sortable but nothing happens... Thank you~

解决方案

Both console.log show the same array because when you use console.log(sortable), sortable is passed by reference, and console output happens AFTER finishing your script - when sortable has been already sorted.

Making your code simple:

var arr = [3,2,1];
console.log(arr); // Produces `[1,2,3]` because its printed
                  // to the console after `arr.sort();`
arr.sort();
console.log(arr); // Produces `[1,2,3]`, as expected

Demo: http://jsfiddle.net/Rfwph/


Workaround

If you want to be able to do console.log with an array to see it before being modifyed, you can use .slice(0) to copy the array, i.e. to obtain another array which contains the same elements as your array.

var arr = [3,2,1];
console.log(arr.slice(0)); // [3,2,1]
arr.sort();
console.log(arr); // [1,2,3]

Demo: http://jsfiddle.net/Rfwph/2/

Edit: better use .slice(0) instead of .slice(), which is supported on FF but ecma262 spec says only end argument is optional.

这篇关于JS对象排序日期排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆