ctrlprogram
Removing Multiple Elements from Array in Javascript
Expelling Multiple Elements
​
One thing lacking from Array.indexOf() is the capacity to look for various things without a moment's delay, so on the off chance that we needed to help that utilization, we'd need to utilize our own circling component. That is the place the second form of Array.remove() comes in. This one acknowledges a second boolean parameter to determine that we need all events of coordinating qualities. There are two exceptionally huge focuses to see in the accompanying for a circle:
It moves in reverse. Something else, the file will get to the wrong component in the wake of expelling a thing.
There is no checking condition. An amusing thing concerning JS is that the circle will exit once the iterator checks down to zero.
on the off chance that (!Array.prototype.remove) {
Array.prototype.remove = function(val, all) {
var I, removedItems = [];
assuming (all) {
for(i = this.length; i– ;){
on the off chance that (this[i] === val) removedItems.push(this.splice(i, 1));
}
}
else {/same as previously…
I = this.indexOf(val);
if(i>-1) removedItems = this.splice(i, 1);
}
return removed items;
};
}
var a = [1,2,3,2,4];
var removedItems = a.remove(2);/a = [1,3,2,4], removedItems = [2];
var b = [1,2,3,2,4];
removedItems = b.remove(2, genuine);/b = [1,3,4], removedItems = [2,2];