Giả sử chúng ta có mảng phức tạp như sau:
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" }
] }
]
}
]
}
}];
Cần viết hàm nhận mảng như vậy. Hàm thêm trường "id" vào tất cả đối tượng có trường "title". Giá trị "id" không quan trọng (bất kỳ giá trị duy nhất nào), quan trọng là mọi đối tượng có "title" phải có "id". Phải làm mà không tạo bản sao mảng gốc.
Ví dụ
Mã thực hiện như sau:
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));
Kết quả
Kết quả trong console sẽ là:
[
{
"Arts": [
{
"Performing arts": [
{
"Music": [
{
"title": "Accompanying",
"id": 1
},
{
"title": "Chamber music",
"id": 2
},
...
]
}
]
}
]
}
}]