我正在尝试转换这个对象的数据数组
const data = [
{
userid: 100,
text: 'yea',
new: true,
datetime: 2018-09-04T20:21:17.000Z
},
{
userid: 101,
text: 'its heading to another new year... wow the time flies...',
new: true,
datetime: 2018-09-03T21:51:27.000Z
},
{
userid: 102,
text: 'wish you the same ...thanks',
new: true,
datetime: 2018-01-12T01:36:28.000Z
}
]
进入以下结构-Object of Objects
{
100: {
userid: 100,
text: 'yea',
new: true,
datetime: 2018-09-04T20:21:17.000Z
},
101: {
userid: 101,
text: 'its heading to another new year... wow the time flies...',
new: true,
datetime: 2018-09-03T21:51:27.000Z
},
102: {
userid: 102,
text: 'wish you the same ...thanks',
new: true,
datetime: 2018-01-12T01:36:28.000Z
}
}
我试过了
messages.map((data) => ({[Math.round(new Date(data.datetime).getTime() / 1000)]: data}))
我得到结构,但map返回一个数组,我需要一个对象。一直在玩reduce()-还不走运。。
谢谢你