将数组分组为几个月和几天

时间:2017-10-03 09:27:47

标签: javascript arrays ecmascript-6 lodash

我正在寻找一种优雅的方式来按月分组或重新组织一个数组,然后将嵌套的日子分组。

示例来自:

[
  {
    "_id": "59d34fabe0a967636c7642f2",
    "date": "2017-02-12",
    "isActive": true
  },
  {
    "_id": "59d34fab9d1a05ca06ef9775",
    "date": "2017-02-12",
    "isActive": true
  },
  {
    "_id": "59d34fab4d49dc45357ab3a5",
    "date": "2017-02-16",
    "isActive": true
  },
  {
    "_id": "59d34fab5aa903089e1f9a44",
    "date": "2017-03-21",
    "isActive": true
  }
]

示例:

{
  '2017-02': [
    {
      "day": "2017-02-12",
      "content": [
        {
          "_id": "59d34fabe0a967636c7642f2",
          "date": "2017-02-12",
          "isActive": true
        },
        {
          "_id": "59d34fab9d1a05ca06ef9775",
          "date": "2017-02-12",
          "isActive": true
        }
    },
    {
      "day": "2017-02-16",
      "content": [
        {
          "_id": "59d34fabe0a967636c7642f2",
          "date": "2017-02-16",
          "isActive": true
        }
    },
  ],
  '2017-03': [
    {
      "day": "2017-03-21",
      "content": [
        {
          "_id": "59d34fabe0a967636c7642f2",
          "date": "2017-03-21",
          "isActive": true
        }
    }, 
}

起初,我使用了groupBy:我得到了第一部分,但不确定如何以优雅的方式获得小组日:

groupBy(array, (item) => {
  return item.date.substring(0,7);
};

现在小组日怎么样?提前致谢。

3 个答案:

答案 0 :(得分:3)

您可以使用嵌套哈希表按月和日进行分组。

var data = [{ _id: "59d34fabe0a967636c7642f2", date: "2017-02-12", isActive: true }, { _id: "59d34fab9d1a05ca06ef9775", date: "2017-02-12", isActive: true }, { _id: "59d34fab4d49dc45357ab3a5", date: "2017-02-16", isActive: true }, { _id: "59d34fab5aa903089e1f9a44", date: "2017-03-21", isActive: true }],
    result = {},
    hash = Object.create(null);

data.forEach(function (o) {
    var month = o.date.slice(0, 7),
        day = o.date.slice(-2);

    if (!hash[month]) {
        hash[month] = { _: [] };
        result[month] = hash[month]._;
    }
    if (!hash[month][day]) {
        hash[month][day] = [];
        result[month].push({ day: o.date, content: hash[month][day] });
    }
    hash[month][day].push(o);
});

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

答案 1 :(得分:1)

我建议反之亦然:先按天计算,然后按月计算:

var array = [{ _id: "59d34fabe0a967636c7642f2", date: "2017-02-12", isActive: true }, { _id: "59d34fab9d1a05ca06ef9775", date: "2017-02-12", isActive: true }, { _id: "59d34fab4d49dc45357ab3a5", date: "2017-02-16", isActive: true }, { _id: "59d34fab5aa903089e1f9a44", date: "2017-03-21", isActive: true }];

result = _(array)
    .groupBy('date')
    .map((content, day) => ( {day, content} ))
    .groupBy($ => $.day.slice(0, 7))
    .value();


console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

答案 2 :(得分:1)

您可以使用updateforEachgroupBy方法执行此操作:

&#13;
&#13;
var arr=[
  {
    "_id": "59d34fabe0a967636c7642f2",
    "date": "2017-02-12",
    "isActive": true
  },
  {
    "_id": "59d34fab9d1a05ca06ef9775",
    "date": "2017-02-12",
    "isActive": true
  },
  {
    "_id": "59d34fab4d49dc45357ab3a5",
    "date": "2017-02-16",
    "isActive": true
  },
  {
    "_id": "59d34fab5aa903089e1f9a44",
    "date": "2017-03-21",
    "isActive": true
  }
]

var groupResult = _.groupBy(arr, item => {
	return item.date.split('-').splice(0,2).join('-')
});

_.forEach(
  groupResult,
  (dates, key) => _.update(
    groupResult,
    key,
    datesArray => _.groupBy(datesArray, item => item.date)
  )
);

console.log('groupResult ==> ', groupResult);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
&#13;
&#13;
&#13;