如何将集合子数组对象与其他集合中存在的实际对象聚合在一起?
例如:This is how my collections look like
Collection 1 - (Teams)个
const Teams = [
{
_id: '60f3b0b3f0b9a1b0b4f9b1a1',
teamName: 'World XI A',
totalPlayers: 15,
player: [
{
playerId: '60f3b0b3f0b9a1b0b4f9b1a2',
playerName: 'Player A',
score: 10,
},
{
playerId: '60f3b0b3f0b9a1b0b4f9b1a3',
playerName: 'Player B',
score: 20,
},
],
},
{
_id: '60f3b0b3f0b9a1b0b4f9b1a4',
teamName: 'Crackers',
totalPlayers: 15,
player: [
{
playerId: '60f3b0b3f0b9a1b0b4f9b1a5',
playerName: 'Player C',
score: 30,
},
{
playerId: '60f3b0b3f0b9a1b0b4f9b1a6',
playerName: 'Player D',
score: 40,
},
],
},
];
Collection 2 - (Players)个
const Players = [
{
_id: '60f3b0b3f0b9a1b0b4f9b1a2',
playerName: 'Player A',
rank: 2,
totalRuns: 5000,
},
{
_id: '60f3b0b3f0b9a1b0b4f9b1a3',
playerName: 'Player B',
rank: 1,
totalRuns: 3050,
},
{
_id: '60f3b0b3f0b9a1b0b4f9b1a5',
playerName: 'Player C',
rank: 3,
totalRuns: 2050,
},
{
_id: '60f3b0b3f0b9a1b0b4f9b1a6',
playerName: 'Player D',
rank: 4,
totalRuns: 1000,
},
];
结果:This is how the resultant array should look like人
Resultant array个
const result = [
{
_id: '60f3b0b3f0b9a1b0b4f9b1a1',
teamName: 'World XI A',
totalPlayers: 15,
player: [
{
playerInfo: {
_id: '60f3b0b3f0b9a1b0b4f9b1a2',
playerName: 'Player A',
rank: 2,
totalRuns: 5000,
},
playerName: 'Player A',
score: 10,
},
{
playerInfo: {
_id: '60f3b0b3f0b9a1b0b4f9b1a3',
playerName: 'Player B',
rank: 1,
totalRuns: 3050,
},
playerName: 'Player B',
score: 20,
},
],
},
{
_id: '60f3b0b3f0b9a1b0b4f9b1a4',
teamName: 'Crackers',
totalPlayers: 15,
player: [
{
playerInfo: {
_id: '60f3b0b3f0b9a1b0b4f9b1a5',
playerName: 'Player C',
rank: 3,
totalRuns: 2050,
},
playerName: 'Player C',
score: 30,
},
{
playerInfo: {
_id: '60f3b0b3f0b9a1b0b4f9b1a6',
playerName: 'Player D',
rank: 4,
totalRuns: 1000,
},
playerName: 'Player D',
score: 40,
},
],
},
];