This question already has an answer here:
这个问题在这里已有答案:
- How to remove item from array by value? 35 answers
如何按值从数组中删除项? 35个答案
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.
我想使用它的值而不是索引来从数组中删除一个项目,这将在IE8上工作。任何帮助将不胜感激。谢谢。
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.
我试过这个,但它不能在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));
}
}
});
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.
也在小提琴。
#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.
也在小提琴。