I want to delete one item from the array using its value instead of index which will work on IE8. Any help will be appreciated. Thanks.
Here is my array:
var myArray = ['one', 'two', 'three'];
The result should be something like:
delete operation:
myArray.splice('three');
result:
myArray =['one', 'two'];
I tried this but its not working in IE8.
angular.forEach($scope.leftList, function (leftItems) {
var arrlen = $scope.rightList.length;
for (var j = 0; j<arrlen; j++) {
if (leftItems == $scope.rightList[j]) {
$scope.rightList = $scope.rightList.slice(0, j).concat($scope.rightList.slice(j+1, arrlen));
}
}
});
You can play with split and join methods and in the right moment also include some regex replace:
var myArray = ['one item', 'two', 'three', 'two', 'two', 'two', 'two', 'fourth item'];
function clearFromArray(item, array) {
var re1 = new RegExp(item,"g");
var re2 = new RegExp('(##)+',"g");
return array.join('##').replace(re1, '').replace(re2, '##').split('##');
};
document.getElementById('result').innerHTML = clearFromArray('two', myArray);
<div id="result"></div>
Also on Fiddle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With