gpt4 book ai didi

javascript - 使用 groupBy 和 map 使用 LoDash 和 Moment 转换数据

转载 作者:行者123 更新时间:2023-12-04 19:52:44 25 4
gpt4 key购买 nike

我对 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();

最佳答案

我确实回答了 very similar question不过昨天我还是会在这里通过修改上一个来提供答案

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>格式。查看更多关于格式化 in the documentation .然后将此值转换为大写以将“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"
}]
}

获取键并将它们转换为属性和值并作为另一个属性添加是一件简单的事情。

关于javascript - 使用 groupBy 和 map 使用 LoDash 和 Moment 转换数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39626665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com