
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Adding a unique id for each entry in JSON object in JavaScript
Suppose, we have an array described as follows −
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" } ] } ] }] }];
We are required to write a JavaScript function that takes in one such array. Then the function should add an "id" field in all those objects that have a "title" field.
The value of "id" property is not of much importance (it can be any unique value), what’s more important is that all those objects with "title" property must have an "id" property.
We have to do this without creating a copy of the actual array.
Example
The code for this will be −
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));
Output
And the output in the console will be −
[ { "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" } ] } ] } ] } ]
- Related Questions & Answers
- How to create a unique ID for each object in JavaScript?
- Search by id and remove object from JSON array in JavaScript
- Sorting a JSON object in JavaScript
- Flattening a JSON object in JavaScript
- JavaScript Adding array name property to JSON object [ES5] and display in Console?
- JSON group object in JavaScript
- Constructing a nested JSON object in JavaScript
- How to add a unique id for an element in HTML?
- How to specify a unique id for an element in HTML?
- Get value for key from nested JSON object in JavaScript
- MySQL query to get count of each fileid entry in a table with Id and FileIDs?
- Removing property from a JSON object in JavaScript
- Updating last entry of a particular ID in MySQL
- Print JSON nested object in JavaScript?
- Search a complex object by id property in JavaScript
Advertisements