按动态键对嵌套对象数组进行排序

时间:2018-08-07 20:54:50

标签: javascript lodash

我相信我的措词正确。...我有一个像这样的数据集...

[
  {
    "Fri Aug 03 2018 00:00:00 GMT-0500": {
      "bill": "Test 2",
      "account": "defaultAccount",
      "amount": 40,
      "category": "defaultCategory",
      "description": "231"
    }
  },
  {
    "Fri Aug 17 2018 00:00:00 GMT-0500": {
      "bill": "Test 2",
      "account": "defaultAccount",
      "amount": 40,
      "category": "defaultCategory",
      "description": "231"
    }
  },
  {
    "Wed Aug 01 2018 00:00:00 GMT-0500": {
      "bill": "test",
      "account": "create-account",
      "amount": 40,
      "category": "mortgage",
      "description": "23"
    }
  }, ....
]

,我正在努力根据对象的动态日期对数组进行排序。 我已经花了最后几个小时来研究堆栈溢出问题的每个版本,但我没有提出解决方案...非常感谢任何建议!

4 个答案:

答案 0 :(得分:4)

您需要获取每个外部对象的键并获取一个日期对象。然后将增量进行排序。

var array = [{ "Fri Aug 03 2018 00:00:00 GMT-0500": { bill: "Test 2", account: "defaultAccount", amount: 40, category: "defaultCategory", description: "231" } }, { "Fri Aug 17 2018 00:00:00 GMT-0500": { bill: "Test 2", account: "defaultAccount", amount: 40, category: "defaultCategory", description: "231" } }, { "Wed Aug 01 2018 00:00:00 GMT-0500": { bill: "test", account: "create-account", amount: 40, category: "mortgage", description: "23" } }];

array.sort((a, b) => new Date(Object.keys(a)[0]) - new Date(Object.keys(b)[0]));

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

我将在下面使用Renderer2函数进行操作:

import _ from "lodash";

var array = [
  {
    "Fri Aug 03 2018 00:00:00 GMT-0500": {
      bill: "Test 2",
      account: "defaultAccount",
      amount: 40,
      category: "defaultCategory",
      description: "231"
    }
  },
  {
    "Fri Aug 17 2018 00:00:00 GMT-0500": {
      bill: "Test 2",
      account: "defaultAccount",
      amount: 40,
      category: "defaultCategory",
      description: "231"
    }
  },
  {
    "Wed Aug 01 2018 00:00:00 GMT-0500": {
      bill: "test",
      account: "create-account",
      amount: 40,
      category: "mortgage",
      description: "23"
    }
  }
];

const result = _.sortBy(array, [
  function(o) {
    return new Date(Object.keys(o)[0]);
  }
]);

console.log(result);

sortBy链接。

答案 2 :(得分:1)

尝试对对象进行格式化,如下所示:

[{
    "createdon": "Fri Aug 03 2018 00:00:00 GMT-0500",
    "bill": "Test 2",
    "account": "defaultAccount",
    "amount": 40,
    "category": "defaultCategory",
    "description": "231"
  },
  {
    "createdon": "Fri Aug 17 2018 00:00:00 GMT-0500",
    "bill": "Test 2",
    "account": "defaultAccount",
    "amount": 40,
    "category": "defaultCategory",
    "description": "231"

  },
  {
    "createdon": "Wed Aug 01 2018 00:00:00 GMT-0500",
    "bill": "test",
    "account": "create-account",
    "amount": 40,
    "category": "mortgage",
    "description": "23"
  }
]

这更具逻辑性,因为数组中的索引已经是标识每个对象的键,因此您无需使用日期作为键。

这是一个完整的示例:

var data = [{
    "createdon": "Fri Aug 03 2018 00:00:00 GMT-0500",
    "bill": "Test 2",
    "account": "defaultAccount",
    "amount": 40,
    "category": "defaultCategory",
    "description": "231"
  },
  {
    "createdon": "Fri Aug 17 2018 00:00:00 GMT-0500",
    "bill": "Test 2",
    "account": "defaultAccount",
    "amount": 40,
    "category": "defaultCategory",
    "description": "231"

  },
  {
    "createdon": "Wed Aug 01 2018 00:00:00 GMT-0500",
    "bill": "test",
    "account": "create-account",
    "amount": 40,
    "category": "mortgage",
    "description": "23"
  }
]

result = _.sortBy(data, [
  function(o) {
    return new Date(o.createdon);
  }
])

即使其他答案可行,此方法也应产生更清晰,更清晰和更可维护的代码,这也是编程的重要组成部分。

答案 3 :(得分:0)

这是使用 lodash sortBy / head / keys来非常简洁地实现此目的的一种方法:

var array = [{ "Fri Aug 03 2018 00:00:00 GMT-0500": { bill: "Test 2", account: "defaultAccount", amount: 40, category: "defaultCategory", description: "231" } }, { "Fri Aug 17 2018 00:00:00 GMT-0500": { bill: "Test 2", account: "defaultAccount", amount: 40, category: "defaultCategory", description: "231" } }, { "Wed Aug 01 2018 00:00:00 GMT-0500": { bill: "test", account: "create-account", amount: 40, category: "mortgage", description: "23" } } ];

console.log(_.sortBy(array, (x) => new Date(_.head(_.keys(x)))));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

我个人认为,由于在这个问题中允许使用lodash,所以这是一种更清洁的衬套解决方案。