জাভাস্ক্রিপ্ট অ্যারে filter() মেথড প্রদত্ত ফাংশন দ্বারা বাস্তবায়ন করা টেস্ট পাস করা সকল এলিমেন্ট সহ একটি নতুন অ্যারে তৈরি করে।
নিচে প্যারামিটারগুলো দেওয়া হলো −
callback − অ্যারে-এর প্রতিটি এলিমেন্ট টেস্ট করার ফাংশন।
thisObject − কলব্যাক এক্সিকিউট করার সময় this হিসেবে ব্যবহার করার অবজেক্ট।
জাভাস্ক্রিপ্টে filter() মেথড কীভাবে কাজ করে শিখতে নিচের কোডটি চালান −
উদাহরণ
<html> <head> <title>JavaScript Array filter Method</title> </head> <body> <script> if (!Array.prototype.filter) { Array.prototype.filter = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); var res = new Array(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) { var val = this[i]; // in case fun mutates this if (fun.call(thisp, val, i, this)) res.push(val); } } return res; }; } function isBigEnough(element, index, array) { return (element >= 10); } var filtered = [12, 5, 8, 130, 44].filter(isBigEnough); document.write("Filtered Value : " + filtered ); </script> </body> </html>