使用Lodash映射时的关键问题

时间:2018-07-22 17:41:16

标签: javascript lodash

我正在尝试获取月度销售额并将其显示在图表上。他们将按年份分组。这是我到目前为止所做的:

unsafe

我的数据:which means 我的输出:the Numpy docs

我的问题是https://pastebin.com/WhJ0atTX而不是2016、2017、2018。 然后在里面,我需要获取密钥作为月份号。https://jsfiddle.net/asfxtor4/1/。您可以在图片中看到。

这是我期望的输出:I am getting keys 0, 1, 2,我需要获取所有月份的数组,如果仅三月份有数据,那么所有其他数组元素都应该为0。

2 个答案:

答案 0 :(得分:0)

您可以通过添加“条目”来获得具有年份和值的对象数组。

let result = _
      .chain(data)
      .groupBy("_id.year")
      .entries()
      .value()
      .map((item) => ({year: item[0], values: item[1]}));

您将不需要:

result = _.map(result, (yearlyValues, key) => {
 return {
 [key]: _.map(yearlyValues, yearlyValuesItems => {
  return {
    [yearlyValuesItems._id.month]: yearlyValuesItems.count
  };
 })
 };
});

您可以在这里看到一个小提琴:

https://jsfiddle.net/Fresh/yr8bqteL/

希望此信息将帮助您完成这项任务。

答案 1 :(得分:0)

由于您知道自己总是有12个月的时间,因此您可能会花更少的时间去做,

data = [{
    "_id": {
      "month": 12,
      "year": 2018
    },
    "count": 3
  },
  {
    "_id": {
      "month": 11,
      "year": 2018
    },
    "count": 4
  },
  {
    "_id": {
      "month": 9,
      "year": 2018
    },
    "count": 3
  },
  {
    "_id": {
      "month": 8,
      "year": 2018
    },
    "count": 4
  },
  {
    "_id": {
      "month": 5,
      "year": 2018
    },
    "count": 4
  },
  {
    "_id": {
      "month": 7,
      "year": 2018
    },
    "count": 2
  },
  {
    "_id": {
      "month": 4,
      "year": 2018
    },
    "count": 5
  },
  {
    "_id": {
      "month": 9,
      "year": 2017
    },
    "count": 2
  },
  {
    "_id": {
      "month": 8,
      "year": 2017
    },
    "count": 4
  },
  {
    "_id": {
      "month": 11,
      "year": 2017
    },
    "count": 4
  },
  {
    "_id": {
      "month": 10,
      "year": 2017
    },
    "count": 3
  },
  {
    "_id": {
      "month": 4,
      "year": 2017
    },
    "count": 2
  },
  {
    "_id": {
      "month": 2,
      "year": 2017
    },
    "count": 3
  },
  {
    "_id": {
      "month": 6,
      "year": 2017
    },
    "count": 1
  },
  {
    "_id": {
      "month": 1,
      "year": 2018
    },
    "count": 4
  },
  {
    "_id": {
      "month": 2,
      "year": 2018
    },
    "count": 4
  },
  {
    "_id": {
      "month": 3,
      "year": 2018
    },
    "count": 9
  },
  {
    "_id": {
      "month": 10,
      "year": 2018
    },
    "count": 3
  },
  {
    "_id": {
      "month": 3,
      "year": 2017
    },
    "count": 9
  },
  {
    "_id": {
      "month": 7,
      "year": 2017
    },
    "count": 2
  },
  {
    "_id": {
      "month": 12,
      "year": 2017
    },
    "count": 3
  },
  {
    "_id": {
      "month": 3,
      "year": 2016
    },
    "count": 1
  },
  {
    "_id": {
      "month": 1,
      "year": 2017
    },
    "count": 3
  },
  {
    "_id": {
      "month": 6,
      "year": 2018
    },
    "count": 3
  }
]

let result = _
  .chain(data)
  .groupBy("_id.year")
  .reduce((obj, current, index) => {
    var sortedMonths = _.sortBy(current, (v) => v._id.month), monthsArray = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
     _.map(current, (curObj, i) => monthsArray[_.get(sortedMonths, '[' + i + ']._id.month') - 1] = sortedMonths[i].count)
      return _.extend(obj, { [index]: monthsArray })
  }, {})
  .value();

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