3

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));
        }
    }
});
11
  • What if 'three' occurs more than once? Commented May 7, 2015 at 21:28
  • What kind of values do you have in array, words only? One word per item? Commented May 7, 2015 at 21:31
  • "I want to..." - but you've shown no code to suggest you've started on that; how far did you get? Where did you get stuck? Commented May 7, 2015 at 21:32
  • Yes words only but it can contain more than one word per item. Commented May 7, 2015 at 21:33
  • @David. I added my code in question. Its not working in IE8 Commented May 7, 2015 at 21:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.