【问题标题】:lodash add keys to countBy functionlodash 向 countBy 函数添加键
【发布时间】:2017-06-20 09:29:49
【问题描述】:

我有一个数据源如下

[
  {
    "physicalId": 2110,
    "closedDate": "2017-06-25T00:00:00.000Z",
    "allDay": true
  },
  {
    "physicalId": 2111,
    "closedDate": "2017-06-24T00:00:00.000Z",
    "allDay": true
  },
  {
    "physicalId": 2111,
    "closedDate": "2017-06-25T00:00:00.000Z",
    "allDay": true
  },
  {
    "physicalId": 4299,
    "closedDate": "2017-06-24T00:00:00.000Z",
    "allDay": true
  },
  {
    "physicalId": 4299,
    "closedDate": "2017-06-25T00:00:00.000Z",
    "allDay": true
  }
]

而我正在使用lodash v4.17.4以如下方式汇总数据:

[
  {
    "Sat Jun 24 2017 00:00:00 GMT+0000": 2,
    "Sun Jun 25 2017 00:00:00 GMT+0000": 3,
  }
]

但是,我希望它以

格式作为对象返回
[
  {
    date: "Sat Jun 24 2017 00:00:00 GMT+0000"
    total: 2
  },
    {
    date: "Sun Jun 25 2017 00:00:00 GMT+0000"
    total: 3
  }
]

我无法在 lodash 中找到完成此操作的方法。我怎样才能做到这一点?

【问题讨论】:

    标签: javascript lodash


    【解决方案1】:

    你可以在_.countBy之后使用_.map

    _.map(_.countBy(source, "closedDate"), (val, key) => ({ date: key, total: val }))
    

    【讨论】:

    • 我在得到的原始结果中没有可参考的键。它是一个日期和一个数字
    【解决方案2】:

    我不确定,但我认为this mapkey function 会有所帮助

    【讨论】:

      【解决方案3】:

      使用纯js,下面两个reduce()会将原始数据转换成想要的格式:

      let d = [{
          "physicalId": 2110,
          "closedDate": "2017-06-25T00:00:00.000Z",
          "allDay": true
        },
        {
          "physicalId": 2111,
          "closedDate": "2017-06-24T00:00:00.000Z",
          "allDay": true
        },
        {
          "physicalId": 2111,
          "closedDate": "2017-06-25T00:00:00.000Z",
          "allDay": true
        },
        {
          "physicalId": 4299,
          "closedDate": "2017-06-24T00:00:00.000Z",
          "allDay": true
        },
        {
          "physicalId": 4299,
          "closedDate": "2017-06-25T00:00:00.000Z",
          "allDay": true
        }
      ];
      
      let r = [
        ...d.reduce((a, b) => {
          a.set(b.closedDate, a.has(b.closedDate) ? a.get(b.closedDate) + 1 : 1);
          return a;
        }, new Map)
      ].reduce((a, b) => {
        let [date, total] = [...b];
        return a.concat({date, total});
      }, []);
      
      console.log(r);

      【讨论】:

        【解决方案4】:

        我们在这里使用 ES6 语法,带有 .reduce().find() 数组助手。

        我们还使用ES6 spread operator 来更改对象内的现有数据。

        这是工作示例。

        var data = [
          {
            "physicalId": 2110,
            "closedDate": "2017-06-25T00:00:00.000Z",
            "allDay": true
          },
          {
            "physicalId": 2111,
            "closedDate": "2017-06-24T00:00:00.000Z",
            "allDay": true
          },
          {
            "physicalId": 2111,
            "closedDate": "2017-06-25T00:00:00.000Z",
            "allDay": true
          },
          {
            "physicalId": 4299,
            "closedDate": "2017-06-24T00:00:00.000Z",
            "allDay": true
          },
          {
            "physicalId": 4299,
            "closedDate": "2017-06-25T00:00:00.000Z",
            "allDay": true
          }
        ]
        
        const newData = data.reduce((previous, current) => {
          
          const recordExists = previous.find(record => record.date === current.closedDate);
          
          if (!recordExists) {
            return previous =  [ ...previous, { date: current.closedDate, total: 1 } ];
          }
          
          recordExists.total += 1;
          
          return [ ...previous, recordExists ];
        }, []);
        
        console.log(newData);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-12-30
          • 2016-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-02
          • 1970-01-01
          相关资源
          最近更新 更多