應該有更好的解 ,就提供給您參考看看~
const userIds = ['U01', 'U02', 'U03'];
const orderIds = ['T01', 'T02', 'T03', 'T04'];
const userOrders = [
{userId: 'U01', orderIds: ['T01', 'T02']},
{userId: 'U02', orderIds: []},
{userId: 'U03', orderIds: ['T03']},
];
const userData = {
'U01': 'Tom',
'U02': 'Sam',
'U03': 'John'
};
const orderData = {
'T01': {name: 'A', price: 499},
'T02': {name: 'B', price: 599},
'T03': {name: 'C', price: 699},
'T04': {name: 'D', price: 799}
};
console.log(
userOrders.map((objs)=>{
const result={};
result.user={id:objs.userId,name:userData[objs.userId]};
result.orders=objs.orderIds.map((orderId)=>{
const order={};
orderData[orderId].id=orderId;
return orderData[orderId];
});
return result
})
);