কম্পিউটার

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


ধরুন, আমাদের কাছে এই ধরনের বাক্যাংশের একটি রেফারেন্স অ্যারে আছে −

const reference = ["your", "majesty", "they", "are", "ready"];

এবং আমাদের অন্য অ্যারের উপর ভিত্তি করে উপরের অ্যারের কিছু উপাদানের সাথে যোগ দিতে হবে তাই যদি অন্য অ্যারে হয় -

const another = ["your", "they are"];

ফলাফল −

এর মত হবে
result = ["your", "majesty", "they are", "ready"];

এখানে, আমরা উভয় অ্যারের উপাদানগুলির তুলনা করেছি, আমরা প্রথম অ্যারের উপাদানগুলির সাথে যোগ দিতাম যদি তারা দ্বিতীয় অ্যারেতে একসাথে থাকে৷

আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা এই ধরনের দুটি অ্যারে নেয় এবং একটি নতুন যোগ করা অ্যারে প্রদান করে৷

উদাহরণ

const reference = ["your", "majesty", "they", "are", "ready"];
const another = ["your", "they are"];
const joinByReference = (reference = [], another = []) => {
   const res = [];
   const filtered = another.filter(a => a.split(" ").length > 1);
   while(filtered.length) {
      let anoWords = filtered.shift();
      let len = anoWords.split(" ").length;
      while(reference.length>len) {
         let refWords = reference.slice(0,len).join(" ");
         if (refWords == anoWords) {
            res.push(refWords);
            reference = reference.slice(len,reference.length);
            break;
         };
         res.push(reference.shift());
      };
   };
   return [...res, ...reference];
};
console.log(joinByReference(reference, another));

আউটপুট

এটি নিম্নলিখিত আউটপুট −

তৈরি করবে
[ 'your', 'majesty', 'they are', 'ready' ]

  1. জাভাস্ক্রিপ্ট অ্যারে শিফট()

  2. জাভাস্ক্রিপ্ট অ্যারে বিপরীত()

  3. জাভাস্ক্রিপ্টে একটি অ্যারের উপর ভিত্তি করে স্ট্রিং এলোমেলো করা

  4. জাভাস্ক্রিপ্টে একটি অ্যারের উপর ভিত্তি করে স্ট্রিং অক্ষর স্থানান্তর করা