嗨,我有一个学生数组。每个学生可能有多个地址(数组)和多个爱好(数组)。
结构如下:
结构如下:
const studentsList = {
students: [
{
id: '54654',
name: 'Tony',
address: [
{
location: 'SNG',
building: 2,
},
{
location: 'LON',
building: 3,
},
],
hobbies: [
{
name: 'soccer',
id: 123,
},
{
name: 'music',
id: 53,
},
],
},
{
id: '505389',
name: 'Stephen Strange',
address: [
{
location: 'LUX',
building: 8,
},
{
location: 'HK',
building: 25,
},
],
hobbies: [
{
name: 'watch tv',
id: 143,
},
{
name: 'music',
id: 83,
},
],
},
{
id: '34534389',
name: 'Wanda Maximoff',
address: [
{
location: 'HKG',
building: 89,
},
{
location: 'GEN',
building: 79,
},
],
hobbies: [
{
name: 'reading',
id: 45,
},
{
name: 'chess',
id: 37,
},
],
},
],
}
我想将学生列表转换为以下格式
1. 每个学生都有一个兴趣爱好的键,并且每个兴趣爱好变成一个单独的爱好,而不是一个数组
2. 每个学生不再有地址键,但是有位置和建筑键。
如下所示
const studentsList = {
students: [
{
id: '54654',
name: 'Tony',
location: 'SNG',
building: 2,
hobbies: {
name: 'soccer',
id: 123,
},
},
{
id: '54654',
name: 'Tony',
location: 'LON',
building: 3,
hobbies: {
name: 'soccer',
id: 123,
},
},
{
id: '54654',
name: 'Tony',
location: 'SNG',
building: 2,
hobbies: {
name: 'music',
id: 53,
},
},
{
id: '54654',
name: 'Tony',
location: 'LON',
building: 3,
hobbies: {
name: 'music',
id: 53,
},
},
{
id: '505389',
name: 'Stephen Strange',
location: 'LUX',
building: 8,
hobbies: {
name: 'watch tv',
id: 143,
},
},
{
id: '505389',
name: 'Stephen Strange',
location: 'HK',
building: 25,
hobbies: {
name: 'watch tv',
id: 143,
},
},
{
id: '505389',
name: 'Stephen Strange',
location: 'HK',
building: 25,
hobbies: {
name: 'music',
id: 83,
},
},
{
id: '505389',
name: 'Stephen Strange',
location: 'LUX',
building: 8,
hobbies: {
name: 'music',
id: 83,
},
},
{
id: '34534389',
name: 'Wanda Maximoff',
location: 'HKG',
building: 89,
hobbies:{
name: 'reading',
id: 45,
},
},
{
id: '34534389',
name: 'Wanda Maximoff',
location: 'GEN',
building: 79,
hobbies:{
name: 'reading',
id: 45,
},
},
{
id: '34534389',
name: 'Wanda Maximoff',
location: 'HKG',
building: 89,
hobbies:{
name: 'chess',
id: 37,
},
},
{
id: '34534389',
name: 'Wanda Maximoff',
location: 'GEN',
building: 79,
hobbies:{
name: 'chess',
id: 37,
},
}
],
}