সমস্যা বিবরণ
একটি নাম্বার অ্যারে থেকে নির্দিষ্ট এলিমেন্ট সব जगह থেকে রিমুভ করতে হবে। ফ্রেমওয়ার্ক ছাড়া কোর জাভাস্ক্রিপ্ট ব্যবহার করে।
চাইতে হচ্ছে: array.remove(number)
সমাধান
const arr = [2, 5, 9, 1, 5, 8, 5];
const removeInstances = function(el) {
const { length } = this;
for (let i = 0; i < this.length; ) {
if (el !== this[i]) {
i++;
continue;
} else {
this.splice(i, 1);
}
}
if (this.length !== length) {
return true;
}
return false;
};
Array.prototype.removeInstances = removeInstances;
console.log(arr.removeInstances(5)); // true
console.log(arr); // [2, 9, 1, 8]
আউটপুট
true
[ 2, 9, 1, 8 ]
সতর্কতা
Array.prototypeমডিফাই করা সাধারণত সুপারিশ করা হয় না, কারণ এটি লাইব্রেরি конфлиক্ট তৈরি করতে পারে।- বিকল্পভাবে পিউর ফাংশন লিখে নতুন অ্যারে রিটার্ন করা ভালো প্র্যাক্টিস।