0

首先,如果这个问题不相关,我很抱歉。我不熟悉 ecma & javascript。我有 2 个对象。

我想合并它们并使用日期字段进行排序。完美地合并.push()但是当我试图用add_date价值对其进行排序时,我得到了意想不到的结果。

第一个对象:

[
    {
        "type": "Sipari\u015f - # PKXLF1",
        "note": "",
        "add_date": "2019-01-07 01:35:25",
        "value": 448.4
    },
    {
        "type": "Sipari\u015f - # PTO3U8",
        "note": "",
        "add_date": "2019-06-25 20:39:44",
        "value": 472
    },
    {
        "type": "Sipari\u015f - # PTYE1M",
        "note": "",
        "add_date": "2019-07-01 09:56:10",
        "value": 1320.77
    },
    {
        "type": "Sipari\u015f - # PXF079",
        "note": "",
        "add_date": "2019-09-06 16:57:09",
        "value": 1859.68
    },
    {
        "type": "Sipari\u015f - # PZ62E5",
        "note": "",
        "add_date": "2019-10-10 18:13:17",
        "value": 755.2
    }
]

第二个对象;

[
    {
        "type": "Tahsilat - Havale \/ Eft",
        "note": "Ziraat Bankas\u0131 Gelen Havale\/EFT",
        "add_date": "2019-01-22 01:35:25",
        "value": 448.4
    },
    {
        "type": "Tahsilat - Havale \/ Eft",
        "note": "Ziraat Bankas\u0131 Gelen Havale\/EFT",
        "add_date": "2019-08-02 20:24:57",
        "value": 1000
    },
    {
        "type": "Tahsilat - Havale \/ Eft",
        "note": "Ziraat Bankas\u0131 Gelen Havale\/EFT",
        "add_date": "2019-10-04 14:48:55",
        "value": 792
    }
]

到目前为止我已经尝试过什么;

如何按日期属性对数组进行排序

这个问题的公认答案

array.sort(function(a,b){
  // Turn your strings into dates, and then subtract them
  // to get a value that is either negative, positive, or zero.
  return new Date(b.date) - new Date(a.date);
});

我已经尝试过,但它给出了意想不到的结果;

0: {type: "Sipariş - # PKXLF1", note: "", add_date: "2019-01-07 01:35:25", value: 448.4}
1: {type: "Sipariş - # PTO3U8", note: "", add_date: "2019-06-25 20:39:44", value: 472}
2: {type: "Sipariş - # PTYE1M", note: "", add_date: "2019-07-01 09:56:10", value: 1320.77}
3: {type: "Sipariş - # PXF079", note: "", add_date: "2019-09-06 16:57:09", value: 1859.68}
4: {type: "Tahsilat - Havale / Eft", note: "Ziraat Bankası Gelen Havale/EFT", add_date: "2019-01-22 01:35:25", value: 448.4}
5: {type: "Tahsilat - Havale / Eft", note: "Ziraat Bankası Gelen Havale/EFT", add_date: "2019-08-02 20:24:57", value: 1000}

我错过了什么?我的add_date领域有wrong date values吗?

任何帮助都非常有用!

4

3 回答 3

0

对象中的日期字符串采用可以按词法排序的格式,无需将它们转换为日期。

您可以简单地使用扩展语法进行合并,并使用and...进行排序,例如<>

let arrA = [
    {"add_date": "2019-01-07 01:35:25"},
    {"add_date": "2019-06-25 20:39:44"},
    {"add_date": "2019-07-01 09:56:10"},
    {"add_date": "2019-09-06 16:57:09"},
    {"add_date": "2019-10-10 18:13:17"}
];

let arrB = [
    {"add_date": "2019-01-22 01:35:25"},
    {"add_date": "2019-08-02 20:24:57"},
    {"add_date": "2019-10-04 14:48:55"}
]

// Merge and sort with newest date first
let arrC = [...arrA, ...arrB].sort((a, b) => a.add_date < b.add_date?  1 :
                                             a.add_date > b.add_date? -1 : 0);

console.log(arrC);

请注意,这不会复制对象, arrC 的元素是对arrAarrB中相同对象的引用。

如果要将“2019-06-25 20:39:44”之类的字符串转换为日期,请不要使用内置解析器。使用简单的解析函数或库,因为它不是 ECMA-262 支持的格式,因此解析依赖于实现,并且至少一个常见的浏览器将返回无效的日期。

于 2019-10-11T11:51:54.147 回答
-1

您是否命名了合并数组?您粘贴的排序方法有一个名为 date 的属性。您的对象具有属性“add_date”。将数组排序方法更改为如下所示:

const a = [
    {
        "type": "Sipari\u015f - # PKXLF1",
        "note": "",
        "add_date": "2019-01-07 01:35:25",
        "value": 448.4
    },
    {
        "type": "Sipari\u015f - # PTO3U8",
        "note": "",
        "add_date": "2019-06-25 20:39:44",
        "value": 472
    },
    {
        "type": "Sipari\u015f - # PTYE1M",
        "note": "",
        "add_date": "2019-07-01 09:56:10",
        "value": 1320.77
    },
    {
        "type": "Sipari\u015f - # PXF079",
        "note": "",
        "add_date": "2019-09-06 16:57:09",
        "value": 1859.68
    },
    {
        "type": "Sipari\u015f - # PZ62E5",
        "note": "",
        "add_date": "2019-10-10 18:13:17",
        "value": 755.2
    }
];

const b = [
    {
        "type": "Tahsilat - Havale \/ Eft",
        "note": "Ziraat Bankas\u0131 Gelen Havale\/EFT",
        "add_date": "2019-01-22 01:35:25",
        "value": 448.4
    },
    {
        "type": "Tahsilat - Havale \/ Eft",
        "note": "Ziraat Bankas\u0131 Gelen Havale\/EFT",
        "add_date": "2019-08-02 20:24:57",
        "value": 1000
    },
    {
        "type": "Tahsilat - Havale \/ Eft",
        "note": "Ziraat Bankas\u0131 Gelen Havale\/EFT",
        "add_date": "2019-10-04 14:48:55",
        "value": 792
    }
];
const c = [...a, ...b];


c.sort(function(a,b){
  // Turn your strings into dates, and then subtract them
  // to get a value that is either negative, positive, or zero.
  return new Date(a.add_date) - new Date(b.add_date);
});

console.log(c);

于 2019-10-11T08:30:44.543 回答
-1

您必须将日期格式转换为时间戳。然后进行排序

于 2019-10-11T08:32:08.753 回答