কম্পিউটার

জাভাস্ক্রিপ্টে JSON অবজেক্টে প্রতিটি এন্ট্রির জন্য একটি অনন্য আইডি যোগ করা হচ্ছে


ধরুন, আমাদের একটি অ্যারে আছে যা নিম্নরূপ বর্ণনা করা হয়েছে −

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

  1. কিভাবে JSON পাঠ্যকে জাভাস্ক্রিপ্ট JSON অবজেক্টে রূপান্তর করবেন?

  2. জাভাস্ক্রিপ্টে প্রতিটি বস্তুর জন্য একটি অনন্য আইডি কীভাবে তৈরি করবেন?

  3. জাভাস্ক্রিপ্টে নেস্টেড JSON অবজেক্ট থেকে কী-এর মান পান

  4. জাভাস্ক্রিপ্টে একটি JSON অবজেক্ট সমতল করা