मान लीजिए, हमारे पास नंबर्स का एक ऐरे है और हमने इसमें एलिमेंट्स जोड़े हैं। आपको ऐरे से किसी विशिष्ट एलिमेंट को हटाने का एक सरल तरीका निकालना है।
निम्नलिखित वही है जो हम ढूंढ रहे हैं:
array.remove(number);
हमें कोर JavaScript का उपयोग करना है। फ्रेमवर्क की अनुमति नहीं है।
उदाहरण
इसके लिए कोड होगा:
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); }; }; // अगर कोई आइटम हटाया गया तो true लौटाएं, अन्यथा false if(this.length !== length){ return true; }; return false; }; Array.prototype.removeInstances = removeInstances; console.log(arr.removeInstances(5)); console.log(arr);आउटपुट
कंसोल में आउटपुट होगा:
true [ 2, 9, 1, 8 ]