কম্পিউটার

একটি বস্তুর মধ্যে বস্তুর একটি জাভাস্ক্রিপ্ট অ্যারে সমতল করুন


বস্তুর একটি জাভাস্ক্রিপ্ট অ্যারেকে অবজেক্টে ফ্ল্যাট করার জন্য, আমরা একটি ফাংশন তৈরি করেছি যা অবজেক্ট অ্যাসলি আর্গুমেন্টের অ্যারে নেয়। এটি একটি চ্যাপ্টা বস্তু প্রদান করে যার সূচী দ্বারা কী যুক্ত হয়। সময়ের জটিলতা হল O(mn) যেখানে n হল অ্যারের আকার এবং m হল প্রতিটি বস্তুর বৈশিষ্ট্যের সংখ্যা। যাইহোক, এর স্পেস কমপ্লেক্সিটি হল O(n) যেখানে n হল প্রকৃত অ্যারের আকার।

উদাহরণ

//code to flatten array of objects into an object
//example array of objects
const notes = [{
   title: 'Hello world',
   id: 1
}, {
   title: 'Grab a coffee',
   id: 2
}, {
   title: 'Start coding',
   id: 3
}, {
   title: 'Have lunch',
   id: 4
}, {
   title: 'Have dinner',
   id: 5
}, {
   title: 'Go to bed',
   id: 6
}, ];
const returnFlattenObject = (arr) => {
   const flatObject = {};
   for(let i=0; i<arr.length; i++){
      for(const property in arr[i]){
         flatObject[`${property}_${i}`] = arr[i][property];
      }
   };
   return flatObject;
}
console.log(returnFlattenObject(notes));

আউটপুট

নিম্নোক্ত কনসোলে আউটপুট -

[object Object] {
   id_0: 1,
   id_1: 2,
   id_2: 3,
   id_3: 4,
   id_4: 5,
   id_5: 6,
   title_0: "Hello world",
   title_1: "Grab a coffee",
   title_2: "Start coding",
   title_3: "Have lunch",
   title_4: "Have dinner",
   title_5: "Go to bed"
}

  1. জাভাস্ক্রিপ্টে Array.flat() পদ্ধতি।

  2. জাভাস্ক্রিপ্টে Array.prototype.flat()।

  3. কিভাবে জাভাস্ক্রিপ্টের সাথে একটি একক অবজেক্ট অ্যারেতে বস্তুগুলিকে একত্রিত করবেন?

  4. জাভাস্ক্রিপ্টে গাছে বস্তুর সমতল অ্যারে