ধরুন, আমাদের কাছে এই ধরনের বাক্যাংশের একটি রেফারেন্স অ্যারে আছে −
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' ]