I came to know that in IE8 array filter function is not supported. After looking for help on internet I found this - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter
我才知道在IE8中不支持数组过滤功能。在互联网上寻求帮助后我找到了这个 - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter
It suggest that IE8 will work using above code.
它建议IE8将使用上面的代码。
HTML Code:
<body>
<a href="javascript:void(0)" onclick="calculateDiff()">Calculate</a>
</body>
JS Code:
function calculateDiff() {
var arr1 = new Array(1, 2, 3);
var arr2 = new Array(3, 4, 5);
var res = arr1.diff(arr2);
alert(res);
}
Array.prototype.diff = function(a) {
if(!Array.prototype.filter) {
alert("not supported");
Array.prototype.filter = function(fun) {
"use strict";
if(this == null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if(typeof fun != "function")
throw new TypeError();
var res = [];
var thisp = arguments[1];
for(var i = 0; i < len; i++) {
if(i in t) {
var val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t))
res.push(val);
}
}
return res;
};
}
else {
alert("supported");
return this.filter(function(i) {
return !(a.indexOf(i) > -1);
});
}
}
I have implemented the solution in this fiddle - http://jsfiddle.net/7LFMA/
我已经在这个小提琴中实现了解决方案 - http://jsfiddle.net/7LFMA/
What is wrong in the code? Why doesn't it work ?
代码有什么问题?为什么不起作用?
2 个解决方案
#1
3
When running diff
for the first time, it will only alert "Not Supported"
, install the filter
polyfill but do nothing. It just returns undefined
, instead of diffing like it is supposed to do.
第一次运行diff时,它只会提示“不支持”,安装过滤器polyfill但什么都不做。它只返回undefined,而不是像它应该做的那样进行diffing。
Remove the else
, or better just move the filter
installation outside the diff
function - it is not to be a part of it (e.g. in terms of closure, though modern engines will care about this).
删除其他,或者更好的只是将过滤器安装移到diff函数之外 - 它不是它的一部分(例如在闭合方面,尽管现代引擎会关心这一点)。
Also, IE8 does not support the indexOf
method, you will need to put this compat shim in as well.
此外,IE8不支持indexOf方法,您还需要将此compat填充。
if (!Array.prototype.indexOf)
Array.prototype.indexOf = function (searchElement) {…};
if (!Array.prototype.filter)
Array.prototype.filter = function(fun) {…};
Array.prototype.diff = function(a) {
return this.filter(function(i) {
return !(a.indexOf(i) > -1);
});
};
function calculateDiff() {
var arr1 = new Array(1, 2, 3);
var arr2 = new Array(3, 4, 5);
var res = arr1.diff(arr2);
alert(res);
}
#2
1
IE8 does not support indexOf for arrays - you still use it in the code so once you implement filter, it DOES exist and then IE8 will try to use indexOf.
IE8不支持数组的indexOf - 你仍然在代码中使用它,所以一旦你实现过滤器,它就存在然后IE8将尝试使用indexOf。
In your code you do not actually APPLY the filter once you call DIFF but once you do that, you also need to implement indexOf
在您的代码中,一旦调用DIFF,您实际上并不应用过滤器,但是一旦这样做,您还需要实现indexOf
MDN数组indexOf
#1
3
When running diff
for the first time, it will only alert "Not Supported"
, install the filter
polyfill but do nothing. It just returns undefined
, instead of diffing like it is supposed to do.
第一次运行diff时,它只会提示“不支持”,安装过滤器polyfill但什么都不做。它只返回undefined,而不是像它应该做的那样进行diffing。
Remove the else
, or better just move the filter
installation outside the diff
function - it is not to be a part of it (e.g. in terms of closure, though modern engines will care about this).
删除其他,或者更好的只是将过滤器安装移到diff函数之外 - 它不是它的一部分(例如在闭合方面,尽管现代引擎会关心这一点)。
Also, IE8 does not support the indexOf
method, you will need to put this compat shim in as well.
此外,IE8不支持indexOf方法,您还需要将此compat填充。
if (!Array.prototype.indexOf)
Array.prototype.indexOf = function (searchElement) {…};
if (!Array.prototype.filter)
Array.prototype.filter = function(fun) {…};
Array.prototype.diff = function(a) {
return this.filter(function(i) {
return !(a.indexOf(i) > -1);
});
};
function calculateDiff() {
var arr1 = new Array(1, 2, 3);
var arr2 = new Array(3, 4, 5);
var res = arr1.diff(arr2);
alert(res);
}
#2
1
IE8 does not support indexOf for arrays - you still use it in the code so once you implement filter, it DOES exist and then IE8 will try to use indexOf.
IE8不支持数组的indexOf - 你仍然在代码中使用它,所以一旦你实现过滤器,它就存在然后IE8将尝试使用indexOf。
In your code you do not actually APPLY the filter once you call DIFF but once you do that, you also need to implement indexOf
在您的代码中,一旦调用DIFF,您实际上并不应用过滤器,但是一旦这样做,您还需要实现indexOf
MDN数组indexOf