使用groupBy和map来使用LoDash和Moment转换数据
Using groupBy and map to transform data using LoDash and Moment
我对lodash没有经验,但我相信它可以帮助我将数据转换为所需的格式。我已经尝试了文档中描述的不同级别的方法,但我无法理解所有的东西。我看了SO,一些博客和文档。我已经尝试组合groupby和map,但我无法解决这个问题。我也不确定如何记录这些步骤。
这就是我想做的,我想把下面的数组变成后面的数组。有人能给我指个方向吗?
原始数据var mockData = [
{
"notice_title": "Bad news",
"notice_text": "Server is down!",
"start_date": "2016-09-18T04:00:00Z"
},
{
"notice_title": "Weekly Reminder",
"notice_text": "Please read the assignment!",
"start_date": "2016-09-18T04:00:00Z"
},
{
"notice_title": "Sweet",
"notice_text": "This morning, the new edition of our blog hit stands!",
"start_date": "2016-09-19T04:00:00Z"
},
{
"notice_title": "Yeah",
"notice_text": "This is pretty cool",
"start_date": "2016-09-19T04:00:00Z"
}
所需数据var newMockData = [
{
"date": "JAN 18 2016",
"messages": [{
"notice_title": "Bad news",
"notice_text": "Server is down!",
"start_date": "2016-09-18T04:00:00Z"
},
{
"notice_title": "Weekly Reminder",
"notice_text": "Please read the assignment!",
"start_date": "2016-09-18T04:00:00Z"
}],
"date": "JAN 19 2016",
"messages": [{
"notice_title": "Sweet",
"notice_text": "This morning, the new edition of our blog hit stands!",
"start_date": "2016-09-19T04:00:00Z"
},
{
"notice_title": "Yeah",
"notice_text": "This is pretty cool",
"start_date": "2016-09-19T04:00:00Z"
}]
}]
更新lodash
var result = _.chain(mockData)
.groupBy(function(item) {
return moment(item.start_date.substring(0,10)).format("MMM-DD-YYYY");
})
.map((value, key) => {
return {
date: key,
param: value
}
})
.value();
我昨天确实回答了一个非常类似的问题,但是,我仍然会通过修改上一个问题来提供一个答案
var mockData = [
{
"notice_title": "Bad news",
"notice_text": "Server is down!",
"start_date": "2016-09-18T04:00:00Z"
},
{
"notice_title": "Weekly Reminder",
"notice_text": "Please read the assignment!",
"start_date": "2016-09-18T04:00:00Z"
},
{
"notice_title": "Sweet",
"notice_text": "This morning, the new edition of our blog hit stands!",
"start_date": "2016-08-19T04:00:00Z"
},
{
"notice_title": "Yeah",
"notice_text": "This is pretty cool",
"start_date": "2016-09-19T04:00:00Z"
}
]
var result = _.chain(mockData)
.groupBy(datum => moment(datum.start_date).format("MMM DD YYYY").toLocaleUpperCase() )
.map((messages, date) => ({ date, messages })) //using ES6 shorthand to generate the objects
.value();
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.1/lodash.min.js"></script>
这里的关键是,当使用_.groupBy
时,您可以提供一个函数来定义如何将对象收集在一起。在本例中,使用Moment.js将消息的start_date
格式化为月-日-年格式:
moment(datum.start_date).format("MMM DD YYYY")`
这将解析消息的日期并以<Short month> <day> <year>
格式输出。在文档中查看有关格式化的更多信息。然后将该值转换为大写以将"Sep"转换为"Sep"
第二件事就是在.map
内部生成新的结构。因为所需的所有信息都已经以如下格式呈现
{
"Sep 18 2016": [{
"notice_title": "Bad news",
"notice_text": "Server is down!",
"start_date": "2016-09-18T04:00:00Z"
}, {
"notice_title": "Weekly Reminder",
"notice_text": "Please read the assignment!",
"start_date": "2016-09-18T04:00:00Z"
}],
"Aug 19 2016": [{
"notice_title": "Sweet",
"notice_text": "This morning, the new edition of our blog hit stands!",
"start_date": "2016-08-19T04:00:00Z"
}],
"Sep 19 2016": [{
"notice_title": "Yeah",
"notice_text": "This is pretty cool",
"start_date": "2016-09-19T04:00:00Z"
}]
}
这是一个简单的问题,获取键并将其转换为属性和值并作为另一个属性添加。
相关文章:
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- Lodash从集合创建集合
- Lodash 从值数组中查找数组中的值
- Moment/Jquery-一个简单时间线的愚蠢问题
- Datetime格式为Friendly Time.Moment JS输出错误
- 问题用moment JS制作一个简单的时间表
- 针对重复发生的事件,使用moment.js防止DST偏移
- Lodash懒惰链不'It’我不管用
- Lodash映射并返回唯一
- 如何在moment.js中只比较日期
- 应该如何在typescript中键入lodash流函数
- 使用lodash的特定对象特性值
- 如何用moment.js列出两个日期之间的所有月份
- 用Jquery map和moment js制作一个简单的时间线
- 如何使用Moment JS获得时间
- 如何获得一个只有时间的moment.js日期对象
- 使用moment.js获取时间分区的偏移日期对象
- Moment js and IOS
- 使用lodash查找具有truthy值的JS对象的属性
- 使用groupBy和map来使用LoDash和Moment转换数据