Computer >> Computer tutorials >  >> Programming >> Javascript

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"
                     }
                  ]
               }
            ]
         }
      ]
   }
]