다음과 같이 설명된 배열이 있다고 가정합니다. -
const arr = [
{
"Arts": [
{
"Performing arts": [
{
"Music": [
{ "title": "Accompanying" },
{ "title": "Chamber music" },
{ "title": "Church music" },
{ "Conducting": [
{ "title": "Choral conducting" },
{ "title": "Orchestral conducting" },
{ "title": "Wind ensemble conducting" }
] },
{ "title": "Early music" },
{ "title": "Jazz studies" },
{ "title": "Musical composition" },
{ "title": "Music education" },
{ "title": "Music history" },
{ "Musicology": [
{ "title": "Historical musicology" },
{ "title": "Systematic musicology" }
] },
{ "title": "Ethnomusicology" },
{ "title": "Music theory" },
{ "title": "Orchestral studies" },
{ "Organology": [
{ "title": "Organ and historical keyboards" },
{ "title": "Piano" },
{ "title": "Strings, harp, oud, and guitar" },
{ "title": "Singing" },
{ "title": "Strings, harp, oud, and guitar" }
] },
{ "title": "Recording" }
] },
{ "Dance": [
{ "title": "Choreography" },
{ "title": "Dance notation" },
{ "title": "Ethnochoreology" },
{ "title": "History of dance" }
] },
{ "Television": [
{ "title": "Television studies" }
] },
{ "Theatre": [
{ "title": "Acting" },
{ "title": "Directing" },
{ "title": "Dramaturgy" },
{ "title": "History" },
{ "title": "Musical theatre" },
{ "title": "Playwrighting" },
{ "title": "Puppetry" }
] }
]
}]
}]; 우리는 그러한 배열을 취하는 JavaScript 함수를 작성해야 합니다. 그런 다음 함수는 "제목" 필드가 있는 모든 개체에 "id" 필드를 추가해야 합니다.
"id" 속성의 값은 그다지 중요하지 않습니다(고유한 값일 수 있음). 더 중요한 것은 "title" 속성이 있는 모든 개체에는 "id" 속성이 있어야 한다는 것입니다.
실제 배열의 복사본을 만들지 않고 이 작업을 수행해야 합니다.
예
이에 대한 코드는 -
const arr = [
{ "Arts": [
{ "Performing arts": [
{ "Music": [
{ "title": "Accompanying" },
{ "title": "Chamber music" },
{ "title": "Church music" },
{ "Conducting": [
{ "title": "Choral conducting" },
{ "title": "Orchestral conducting" },
{ "title": "Wind ensemble conducting" }
] },
{ "title": "Early music" },
{ "title": "Jazz studies" },
{ "title": "Musical composition" },
{ "title": "Music education" },
{ "title": "Music history" },
{ "Musicology": [
{ "title": "Historical musicology" },
{ "title": "Systematic musicology" }
] },
{ "title": "Ethnomusicology" },
{ "title": "Music theory" },
{ "title": "Orchestral studies" },
{ "Organology": [
{ "title": "Organ and historical keyboards" },
{ "title": "Piano" },
{ "title": "Strings, harp, oud, and guitar" },
{ "title": "Singing" },
{ "title": "Strings, harp, oud, and guitar" }
] },
{ "title": "Recording" }
] },
{ "Dance": [
{ "title": "Choreography" },
{ "title": "Dance notation" },
{ "title": "Ethnochoreology" },
{ "title": "History of dance" }
] },
{ "Television": [
{ "title": "Television studies" }
] },
{ "Theatre": [
{ "title": "Acting" },
{ "title": "Directing" },
{ "title": "Dramaturgy" },
{ "title": "History" },
{ "title": "Musical theatre" },
{ "title": "Playwrighting" },
{ "title": "Puppetry" }
] }
]
}]
}];
const addId = (id = 1) => {
return function recur(obj) {
if ('title' in obj) {
obj.id = id++;
};
Object.keys(obj).forEach(el => {
Array.isArray(obj[el]) && obj[el].forEach(recur);
});
};
}
const mapId = arr => {
arr.forEach(addId);
}
mapId(arr);
console.log(JSON.stringify(arr, undefined, 4)); 출력
콘솔의 출력은 -
[
{
"Arts": [
{
"Performing arts": [
{
"Music": [
{
"title": "Accompanying"
},
{
"title": "Chamber music"
},
{
"title": "Church music"
},
{
"Conducting": [
{
"title": "Choral conducting"
},
{
"title": "Orchestral conducting"
},
{
"title": "Wind ensemble conducting"
}
]
},
{
"title": "Early music"
},
{
"title": "Jazz studies"
},
{
"title": "Musical composition"
},
{
"title": "Music education"
},
{
"title": "Music history"
},
{
"Musicology": [
{
"title": "Historical musicology"
},
{
"title": "Systematic musicology"
}
]
},
{
"title": "Ethnomusicology"
},
{
"title": "Music theory"
},
{
"title": "Orchestral studies"
},
{
"Organology": [
{
"title": "Organ and historical keyboards"
},
{
"title": "Piano"
},
{
"title": "Strings, harp, oud, and guitar"
},
{
"title": "Singing"
},
{
"title": "Strings, harp, oud, and guitar"
}
]
},
{
"title": "Recording"
}
]
},
{
"Dance": [
{
"title": "Choreography"
},
{
"title": "Dance notation"
},
{
"title": "Ethnochoreology"
},
{
"title": "History of dance"
}
]
},
{
"Television": [
{
"title": "Television studies"
}
]
},
{
"Theatre": [
{
"title": "Acting"
},
{
"title": "Directing"
},
{
"title": "Dramaturgy"
},
{
"title": "History"
},
{
"title": "Musical theatre"
},
{
"title": "Playwrighting"
},
{
"title": "Puppetry"
}
]
}
]
}
]
}
]