JavaScript使用IE9不支持的splice从数组中查找并删除元素
我有一个JavaScript数组,我需要找到一个特定的元素并删除它。我尝试了JavaScript使用IE9不支持的splice从数组中查找并删除元素,javascript,arrays,internet-explorer-9,array-splice,Javascript,Arrays,Internet Explorer 9,Array Splice,我有一个JavaScript数组,我需要找到一个特定的元素并删除它。我尝试了splice()和findIndex(),JSEclipse和IE9都不支持它。我使用了splice()和find(),但它在IE9中不起作用 我的问题不是重复的原因有两点: (1) 我的数组是一个对象数组,因此使用indexOf()不适用。 (2) IE9中的支持是我的解决方案的先决条件 如果有助手,我将不胜感激 我的阵列: var portingOptions = [ { name: 'print'
splice()
和findIndex()
,JSEclipse和IE9都不支持它。我使用了splice()
和find()
,但它在IE9中不起作用
我的问题不是重复的原因有两点:
(1) 我的数组是一个对象数组,因此使用indexOf()
不适用。
(2) IE9中的支持是我的解决方案的先决条件
如果有助手,我将不胜感激
我的阵列:
var portingOptions = [
{
name: 'print',
iconClass: 'faxBlue'
},
{
name: 'pdf',
iconClass: 'pdfBlue'
},
{
name: 'exportToCcr',
iconClass: 'documentBlue'
},
{
name: 'message',
iconClass: 'secureMessageBlue'
},
{
name: 'email',
iconClass: 'emailBlue'
}
];
我的代码与splice()
和find()
:
有人知道一个可以在IE9上工作的解决方案吗?您可以进行一个while循环,而不是
查找
,因为它没有实现
如果要删除多个语句,请删除break
语句
var index = array.length;
while (index--) {
if (array[index].name === 'email') {
array.splice(index, 1);
break;
}
}
您可以使用jQuery方法替换
数组。find()
Array.splice()
似乎受到IE 5.5的支持
还有一个很好的polyfill可以在IE9上使用:
Array.prototype.find = Array.prototype.find || function(callback) {
if (this === null) {
throw new TypeError('Array.prototype.find called on null or undefined');
} else if (typeof callback !== 'function') {
throw new TypeError('callback must be a function');
}
var list = Object(this);
// Makes sures is always has an positive integer as length.
var length = list.length >>> 0;
var thisArg = arguments[1];
for (var i = 0; i < length; i++) {
var element = list[i];
if ( callback.call(thisArg, element, i, list) ) {
return element;
}
}
Array.prototype.find=Array.prototype.find | |函数(回调){
if(this==null){
抛出新的TypeError('Array.prototype.find调用为null或未定义');
}else if(回调类型!==“函数”){
抛出新类型错误('回调必须是函数');
}
变量列表=对象(此);
//确保始终有一个正整数作为长度。
var length=list.length>>>0;
var thisArg=参数[1];
对于(变量i=0;i
Ref:您可以使用polyfill进行
筛选:它的可能重复项不是重复项,因为我不能使用indexOf()
-我的数组是一个对象数组,使用indexOf())
不适用于我的问题。我只能使用JavaScript,jQuery不包含在我们的项目中。请使用polyFill更新答案。请仔细阅读我的问题:我尝试使用find()
,但IE9不支持它。在JavaScript中从数组中删除元素不是问题,我的挑战是以IE9支持的方式执行。这是解决方案的条件。请仔细阅读我的回答,如果array.find()
不存在,则会应用多边形填充,并且IE9中也会有array.find()
Array.prototype.find = Array.prototype.find || function(callback) {
if (this === null) {
throw new TypeError('Array.prototype.find called on null or undefined');
} else if (typeof callback !== 'function') {
throw new TypeError('callback must be a function');
}
var list = Object(this);
// Makes sures is always has an positive integer as length.
var length = list.length >>> 0;
var thisArg = arguments[1];
for (var i = 0; i < length; i++) {
var element = list[i];
if ( callback.call(thisArg, element, i, list) ) {
return element;
}
}