একটি জাভাস্ক্রিপ্ট অ্যারে ফাংশন লিখতে হবে যা একটি নেস্টেড অ্যারে (যেটিতে false মানও থাকতে পারে) গ্রহণ করবে এবং নেস্টিং ছাড়া সব উপাদান ধারণকারী একটি অ্যারে ফেরত দেবে।
উদাহরণস্বরূপ, যদি ইনপুট হয় −
const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], [6]];
তবে আউটপুট হওয়া উচিত −
const output = [1, 2, 3, 4, 5, false, 6, 5, 8, null, 6];
তাহলে, এই ফাংশনটির কোড লিখি −
উদাহরণ
কোডটি হবে −
const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], [6]]; const flatten = function(){ let res = []; for(let i = 0; i < this.length; i++){ if(Array.isArray(this[i])){ res.push(...this[i].flatten()); }else{ res.push(this[i]); }; }; return res; }; Array.prototype.flatten = flatten; console.log(arr.flatten());আউটপুট
কনসোলে আউটপুট হবে −
[ 1, 2, 3, 4, 5, 5, false, 6, 5, 8, null, 6 ]