मान लीजिए, हमारे पास इस प्रकार वर्णित एक सरणी है -
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 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" } ] } ] } ] } ]