我正在尝试将一个对象数组分类为另一个对象数组,而且还要组合公用键(在这种情况下,键为updatedAt)
示例:
const arr = [
{ id: '5c65ded933767500010c22a6',
createdAt: '2019-02-14T21:34:17.181Z',
updatedAt: '2019-02-14T21:38:01.251Z',
userId: '5b213937cdcd57002d128aa5',
phoneNumber: '(514) 578-9586',
orderId: '5c65ded346e0fb000698f102',
paymentMethods: [],
shopName: 'test1',
amount: 3.47,
type: 'CASHBACK',
status: 'COMPLETED',
currency: 'CAD' },
{ id: '5c5226777de2c4000139de58',
createdAt: '2019-01-30T22:34:31.648Z',
updatedAt: '2019-01-30T22:37:01.246Z',
userId: '5b213937cdcd57002d128aa5',
phoneNumber: '(514) 578-9586',
orderId: '5c52267146e0fb0006dc852a',
paymentMethods: [],
shopName: 'test2',
amount: 0.24,
type: 'CASHBACK',
status: 'COMPLETED',
currency: 'CAD' },
{ id: '5c019c07c9e77c0001c5ceb8',
createdAt: '2018-11-30T20:22:31.392Z',
updatedAt: '2019-01-17T15:53:24.410Z',
userId: '5b213937cdcd57002d128aa5',
phoneNumber: null,
orderId: '5c019bfcc9e77c0006ba4f77',
paymentMethods: [],
shopName: 'test3',
amount: 1.12,
type: 'CASHBACK',
status: 'COMPLETED',
currency: 'CAD' },
{ id: '5c0195cac9e77c0001c5ceb5',
createdAt: '2018-11-30T19:55:54.646Z',
updatedAt: '2019-01-17T15:53:12.624Z',
userId: '5b213937cdcd57002d128aa5',
phoneNumber: null,
orderId: '5c0195bfc9e77c0006b3873f',
paymentMethods: [],
shopName: 'test4',
amount: 1.15,
type: 'CASHBACK',
status: 'COMPLETED',
currency: 'CAD' } ]
进入:
[
{
date: '2019-01-30T22:37:01.246Z',
transactions: [
{'...THE TRANSACTIONS THAT OCURED ON THE SAME DATE...'},
{'...THE TRANSACTIONS THAT OCURED ON THE SAME DATE...'},
{'...THE TRANSACTIONS THAT OCURED ON THE SAME DATE...'},
]
},
{
date: '2019-01-14T22:37:01.246Z',
transactions: [
{'...THE TRANSACTIONS THAT OCURED ON THE SAME DATE...'},
{'...THE TRANSACTIONS THAT OCURED ON THE SAME DATE...'},
{'...THE TRANSACTIONS THAT OCURED ON THE SAME DATE...'},
]
}
]
我尝试过使用多个循环,但是效率似乎低下,所以我后来尝试使用lodash,这似乎也没有效果
这是我尝试使用lodash
const mapDaysToTransactions = (transactions) => {
const mappedTransactions = transactions.reduce((acc, next) => {
return acc.concat({
day: next.updatedAt,
transactions: next
})
}, [])
const groupedBydays = _.groupBy(mappedTransactions, el => moment(el.day).format('MMMM, d'))
return Object.keys(groupedBydays).map(d => {
return {
date: groupedBydays[d][0].day,
transactions: _.flattenDeep(groupedBydays[d])
}
})
}
我通过上面的函数获得了预期的结果,但是我想知道是否有一种“更好”的更佳方法来解决这种情况。
更新:另一种使用单个reduce的解决方案
const mapDaysToTransactions = (transactions) => {
const mapped = transactions.reduce((acc, next) => {
return acc.concat({
day: next.updatedAt,
transactions: next
})
}, [])
return mapped.reduce((acc, transaction) => {
const date = moment(transaction.day).format('MMMM, d');
const iso = transaction.day
const index = acc.findIndex(t => t.date === date);
const idx = acc.indexOf(date)
if (idx !== -1) {
const transactionsByDate = acc[idx];
const newAcc = [
...acc.slice(0, idx),
...acc.slice(idx + 1, acc.length),
]
return newAcc.concat({
date,
day: iso,
transactions: transactionsByDate.transactions.concat(transaction),
});
}
return acc.concat({
date,
day: iso,
transactions: [transaction],
});
}, []);
}
答案 0 :(得分:0)
在reduce
中,您首先要创建一个新数组,而可以使用object
和基于date
的组值,而不是创建数组
const arr = [{ id: '5c65ded933767500010c22a6',createdAt: '2019-02-14T21:34:17.181Z',updatedAt: '2019-02-14T21:38:01.251Z',userId: '5b213937cdcd57002d128aa5',phoneNumber: '(514) 578-9586',orderId: '5c65ded346e0fb000698f102',paymentMethods: [],shopName: 'test1',amount: 3.47,type: 'CASHBACK',status: 'COMPLETED',currency: 'CAD' },{ id: '5c5226777de2c4000139de58',createdAt: '2019-01-30T22:34:31.648Z',updatedAt: '2019-01-30T22:37:01.246Z',userId: '5b213937cdcd57002d128aa5',phoneNumber: '(514) 578-9586',orderId: '5c52267146e0fb0006dc852a',paymentMethods: [],shopName: 'test2',amount: 0.24,type: 'CASHBACK',status: 'COMPLETED',currency: 'CAD' },{ id: '5c019c07c9e77c0001c5ceb8',createdAt: '2018-11-30T20:22:31.392Z',updatedAt: '2019-01-17T15:53:24.410Z',userId: '5b213937cdcd57002d128aa5',phoneNumber: null,orderId: '5c019bfcc9e77c0006ba4f77',paymentMethods: [],shopName: 'test3',amount: 1.12,type: 'CASHBACK',status: 'COMPLETED',currency: 'CAD' },{ id: '5c0195cac9e77c0001c5ceb5',createdAt: '2018-11-30T19:55:54.646Z',updatedAt: '2019-01-17T15:53:12.624Z',userId: '5b213937cdcd57002d128aa5',phoneNumber: null,orderId: '5c0195bfc9e77c0006b3873f',paymentMethods: [],shopName: 'test4',amount: 1.15,type: 'CASHBACK',status: 'COMPLETED',currency: 'CAD' } ]
let final = arr.reduce((op,inp) => {
let date = inp.updatedAt.substr(0,10)
op[date] = op[date] || {date, transaction:[]}
op[date].transaction.push(inp)
return op
},{})
console.log(Object.values(final))