IE8 是否支持 javascript .map() 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7350912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is the javascript .map() function supported in IE8?
提问by Richard H
When doing this:
这样做时:
var a = new Array("a", "b");
a.map(function() { });
in IE8 I get:
在 IE8 中我得到:
"Object doesn't support this property or method"
Is this method not supported in IE8, or do I have some other problem? I've had a Google, but get lots of Google Maps javascript issues/questions...
IE8 不支持此方法,还是我有其他问题?我有一个谷歌,但有很多谷歌地图 javascript 问题/问题......
Edit: OK so IE8 and below DO NOT support the .map() function. Copy-paste the code from MDN herewhich will add the .map() function to the Array prototype exactly per the specs if not natively supported (and it seems to work perfectly).
编辑:好的,所以 IE8 及以下不支持 .map() 函数。将 MDN 中的代码复制粘贴到此处,如果本机不支持,它将完全按照规范将 .map() 函数添加到 Array 原型中(并且它似乎工作得很好)。
回答by Interactive Tribe
The solution is jQuery.map
解决方案是 jQuery.map
Instead of this:
a.map(function( ) { });
取而代之的是:
a.map(function( ) { });
You have to do
你必须要做
jQuery.map(a, function( ) {
//what ever you want todo ..
}
jQuery.map(a, function( ) {
//what ever you want todo ..
}
回答by Justin Niessner
回答by Harry
(function(fn){
if (!fn.map) fn.map=function(f){var r=[];for(var i=0;i<this.length;i++)r.push(f(this[i]));return r}
if (!fn.filter) fn.filter=function(f){var r=[];for(var i=0;i<this.length;i++)if(f(this[i]))r.push(this[i]);return r}
})(Array.prototype);
Put anywhere before first .map or .filter call. Problem solved. jQuery.map() method doesn't work as expected.
放在第一个 .map 或 .filter 调用之前的任何位置。问题解决了。jQuery.map() 方法无法按预期工作。
UPDATE:I've just tested it on sparse arrays: if map or filter argument is a function which accepts and handles undefined
value - it works, but the results are not obvious:
更新:我刚刚在稀疏数组上对其进行了测试:如果 map 或 filter 参数是一个接受和处理undefined
值的函数- 它可以工作,但结果并不明显:
Let's define test sparse array:
让我们定义测试稀疏数组:
var t = []
t[1] = 1; t[3] = 3; t[5] = 5;
Let's see what does IE8 say about t: "[undefined, 1, undefined, 3, undefined, 5]"
来看看IE8对t是怎么说的:“[undefined, 1, undefined, 3, undefined, 5]”
Let's try:
咱们试试吧:
t.filter(function(x){return x<4})
What is it, IE8? It's: "[1, 3]". Note - no undefined values. I would personally expect that.
这是什么,IE8?它是:"[1, 3]"。注意 - 没有未定义的值。我个人希望如此。
But try THIS:
但是试试这个:
t.map(function(x){return 2<<x})
And... "[2, 4, 2, 16, 2, 64]". That's weird! :) Try this:
还有... "[2, 4, 2, 16, 2, 64]"。这很奇怪!:) 尝试这个:
t.map(function(x){return Math.pow(2,x)})
And?... "[NaN, 2, NaN, 8, NaN, 32]"- I would rather expect this result for the previous test. It's at least logical - Math.pow() is supposed to return a number
type, NaN
, regardless of it's meaning IS a special number
type reserved for invalid operations. So the result is more or less correct. It would be fully correct as map
result if t remained a sparse array.
还有?... "[NaN, 2, NaN, 8, NaN, 32]"- 我宁愿期待上次测试的结果。这至少是合乎逻辑的 - Math.pow() 应该返回一个number
类型, NaN
,不管它的含义是number
为无效操作保留的特殊类型。所以结果或多或少是正确的。map
如果 t 仍然是一个稀疏数组,那么结果将是完全正确的。
So without further ado - ultimately correct version of map
and filter
methods:
所以不用多说 -最终正确的版本map
和filter
方法:
(function(fn){
if (!fn.map) fn.map=function(f){var r=[];for(var i=0;i<this.length;i++)if(this[i]!==undefined)r[i]=f(this[i]);return r}
if (!fn.filter) fn.filter=function(f){var r=[];for(var i=0;i<this.length;i++)if(this[i]!==undefined&&f(this[i]))r[i]=this[i];return r}
})(Array.prototype);
And the test:
和测试:
var t = []; t[1] = 1; t[3] = 3; t[5] = 5;
var t1 = t.map(function(x){return 2<<x});
var t2 = t.filter(function(x){return x<10});
console.debug(t);
console.debug(t1);
console.debug(t2);
Expected results:
预期成绩:
[object Array] [undefined, 1, undefined, 3, undefined, 5]
[object Array][undefined, 4, undefined, 16, undefined, 64]
[object Array][undefined, 1, undefined, 3, undefined, 5]
[对象数组] [未定义,1,未定义,3,未定义,5]
[对象数组][未定义,4,未定义,16,未定义,64]
[对象数组][未定义,1,未定义,3,未定义,5]
回答by Oleksandr Tsurika
在 MSDN 上,地图要求:不支持以下文档模式:Quirks、Internet Explorer 6 标准、Internet Explorer 7 标准、Internet Explorer 8 标准。
Map is just a implementation of "Visitor" pattern for array. So easy substitute for it could be:
Map 只是数组的“访问者”模式的实现。如此简单的替代品可能是:
function visitArray(arr, visitor) {
var result = [];
for (var i = 0; i < arr.length; i ++) {
result[i] = visitor(arr[i]);
}
return result;
}
The function also takes array and function which to invoke on each array element. It returns a new array with result of visitor invokation for each original array element
该函数还采用数组和要在每个数组元素上调用的函数。它返回一个新数组,其中包含每个原始数组元素的访问者调用结果