为JavaScript中JSON对象中的每个条目添加唯一的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" }

            ] }

         ]

      }]

}];

我们需要编写一个包含一个这样的数组的JavaScript函数。然后,该函数应在所有具有“标题”字段的对象中添加“ id”字段。

“ 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"

                     }

                  ]

               }

            ]

         }

      ]

   }

]

以上是 为JavaScript中JSON对象中的每个条目添加唯一的ID 的全部内容, 来源链接: utcz.com/z/314420.html

回到顶部