สมมติว่าเรามีอาร์เรย์ที่อธิบายดังนี้ −
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" ในออบเจกต์ทั้งหมดที่มีฟิลด์ "title"
ค่าของคุณสมบัติ "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" } ] } ] } ] } ]