如何转换映射:值键和合并另一个值,把它们作为数组内的键?



我需要合并2个映射,将一些值转换为键,并将另一个值作为数组放入这些键中,例如:

From this maps:

const names= [
{ id: 1, name: 'JULIO FONTOVA' },
{ id: 2, name: 'CHRISTIAN JONES' },
{ id: 3, name: 'MARK DAVIES' }
];
const nexus = [
{ namesId: 1, phoNumbId: 1, country: PERU, mount: 1200 },
{ namesId: 1, phoNumbId: 2, country: CANADA, mount: 2000},
{ namesId: 2, phoNumbId: 2, country: ENGLAND, mount: 3000},
{ namesId: 2, phoNumbId: 3, country: RUSSIA, mount: 40000},
{ namesId: 3, phoNumbId: 1, country: BELGIUM, mount: 500},
{ namesId: 3, phoNumbId: 2, country: SPAIN, mount: 500},
{ namesId: 3, phoNumbId: 3, country: PORTUGAL, mount: 2020}
]
const phoneNumbers= [
{ id: 1, phoNumb: '111', name: 'JHON EVANS'},
{ id: 2, phoNumb: '222', name: 'JUDITH SOTO'},
{ id: 3, phoNumb: '333', name: 'OSCAR CIENFUEGOS'},
{ id: 4, phoNumb: '444', name: 'ANDREW JONES'}
]

我尝试了许多解决方法,但没有一个奏效。如果有人能帮我解决这个问题,我会很感激的。我想把它们转换成这样:

const mergedNumbers = [
{JULIO FONTOVA : [111,222]},
{CHRISTIAN JONES : [222,333]},
{MARK DAVIES : [111,222,333]}
]

就是这样:

const names = [
{ id: 1, name: "JULIO FONTOVA" },
{ id: 2, name: "CHRISTIAN JONES" },
{ id: 3, name: "MARK DAVIES" },
];
const nexus = [
{ namesId: 1, phoNumbId: 1, country: "PERU", mount: 1200 },
{ namesId: 1, phoNumbId: 2, country: "CANADA", mount: 2000 },
{ namesId: 2, phoNumbId: 2, country: "ENGLAND", mount: 3000 },
{ namesId: 2, phoNumbId: 3, country: "RUSSIA", mount: 40000 },
{ namesId: 3, phoNumbId: 1, country: "BELGIUM", mount: 500 },
{ namesId: 3, phoNumbId: 2, country: "SPAIN", mount: 500 },
{ namesId: 3, phoNumbId: 3, country: "PORTUGAL", mount: 2020 },
];
const phoneNumbers = [
{ id: 1, phoNumb: "111", name: "JHON EVANS" },
{ id: 2, phoNumb: "222", name: "JUDITH SOTO" },
{ id: 3, phoNumb: "333", name: "OSCAR CIENFUEGOS" },
{ id: 4, phoNumb: "444", name: "ANDREW JONES" },
];
const output = [];
for (const element of names) {
output.push({
[element.name]: nexus
.filter((e) => e.namesId === element.id)
.map((e) => phoneNumbers.find((f) => f.id === e.phoNumbId).phoNumb),
});
}
console.log(output);

相关内容

  • 没有找到相关文章

最新更新