2

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));
        }
    }
});
Sampson
  • 251,934
  • 70
  • 517
  • 549
user3842029
  • 195
  • 1
  • 1
  • 10

1 Answers1

1

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.

skobaljic
  • 8,855
  • 1
  • 23
  • 47