最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - lodash add keys to countBy function - Stack Overflow

programmeradmin3浏览0评论

I have a datasource as follows

[
  {
    "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
  }
]

and I'm using lodash v4.17.4 to summarize the data in the following way:

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

However, i would like this returned as object in the format of

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

I've not been able to find a way to plete this in lodash. How can I achieve this?

I have a datasource as follows

[
  {
    "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
  }
]

and I'm using lodash v4.17.4 to summarize the data in the following way:

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

However, i would like this returned as object in the format of

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

I've not been able to find a way to plete this in lodash. How can I achieve this?

Share Improve this question asked Jun 20, 2017 at 9:29 Colonel MustardColonel Mustard 1,5332 gold badges19 silver badges44 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

You can use _.map after _.countBy:

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

I am not sure but i think this mapkey function would help

Using plain js, the following two reduce() would convert the original data to the desired format:

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);

We are using here ES6 syntax, with .reduce() and .find() array helpers.

We are also using ES6 spread operator to change existing data inside our objects.

Here is working example.

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);

发布评论

评论列表(0)

  1. 暂无评论