在IE(Javascript)中将对象转换为数组 [英] Convert object into array in IE (Javascript)

查看:557
本文介绍了在IE(Javascript)中将对象转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用Javascript将对象转换为Internet Explorer中的数组吗?我读到IE不支持Array.from(obj)方法。



谢谢

解决方案

您可以验证在



在同一页面上,您还可以找到以下 polyfill Array.from()的支持添加到本机不支持的浏览器中:

  // ECMA-262版本6,22.1.2.1 
if(!Array.from){
Array.from =(function (){
var toStr = Object.prototype.toString;
var isCallable =函数(fn){
返回typeof fn = ==功能 || toStr.call(fn)===‘[对象功能]’;
};
var toInteger =函数(值){
var number = Number(值);
if(isNaN(number)){返回0; }
if(number === 0 ||!isFinite(number)){返回数字; }
return(number> 0?1:-1)* Math.floor(Math.abs(number));
};
var maxSafeInteger = Math.pow(2,53)-1;
var toLength =函数(值){
var len = toInteger(value);
返回Math.min(Math.max(len,0),maxSafeInteger);
};

// from方法的length属性为1。
返回函数from(arrayLike / *,mapFn,thisArg * /){
// 1.令C为这个值。
var C = this;

// 2.令项目为ToObject(arrayLike)。
var items = Object(arrayLike);

// 3. ReturnIfAbrupt(items)。
if(arrayLike == null){
抛出新的TypeError(‘Array.from需要类似数组的对象-不能为null或未定义’);
}

// 4.如果未定义mapfn,则使映射为false。
var mapFn = arguments.length> 1个arguments [1]:无效未定义;
var T;
if(typeof mapFn!=='undefined'){
// 5. else
// 5. a如果IsCallable(mapfn)为false,则抛出TypeError异常。
if(!isCallable(mapFn)){
抛出新的TypeError(’Array.from:提供时,第二个参数必须是一个函数’);
}

// 5。如果提供了thisArg,则让T为thisArg;否则让T不确定。
if(arguments.length> 2){
T = arguments [2];
}
}

// 10.令lenValue为Get(items, length)。
// 11.令len为ToLength(lenValue)。
var len = toLength(items.length);

// 13.如果IsConstructor(C)为true,则
// 13. a。令A为使用包含单个项目len的参数列表调用C的[[Construct]]内部方法
//的结果。
// 14。否则,让A为ArrayCreate(len)。
var A = isCallable(C)吗? Object(new C(len)):新的Array(len);

// 16.令k为0。
var k = 0;
// 17.重复,而k< len…(以及步骤a-h)
var kValue;
而(k kValue = items [k];
if(mapFn){
A [k] = typeof T ==='undefined'吗? mapFn(kValue,k):mapFn.call(T,kValue,k);
} else {
A [k] = kValue;
}
k + = 1;
}
// //让putStatus为Put(A, length,len,true)。
A.length = len;
// 20.返回A。
返回A;
};
}());
}

请注意,它带有以下注释:


该算法与
完全相同,假定对象
和TypeError具有其原始值和该回调,这是ECMA-262第6版中指定的算法。调用
计算得出Function.prototype.call的原始值。另外,由于
无法真正填充可迭代对象,因此
实现不支持ECMA-262第6版中
定义的通用可迭代对象。


这意味着有一些警告,但对于大多数用途而言,这已经足够了!


can I convert , in Javascript, an object into an array in Internet Explorer? I read that the method Array.from(obj) is not supported from IE. It is correct?

Thank you

解决方案

You can verify for yourself on On Mozilla's MDN that Array.from() isn't supported by IE :

On that same page, you can also find the following polyfill to add support of Array.from() to browsers that don't support it natively :

// Production steps of ECMA-262, Edition 6, 22.1.2.1
if (!Array.from) {
  Array.from = (function () {
    var toStr = Object.prototype.toString;
    var isCallable = function (fn) {
      return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
    };
    var toInteger = function (value) {
      var number = Number(value);
      if (isNaN(number)) { return 0; }
      if (number === 0 || !isFinite(number)) { return number; }
      return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
    };
    var maxSafeInteger = Math.pow(2, 53) - 1;
    var toLength = function (value) {
      var len = toInteger(value);
      return Math.min(Math.max(len, 0), maxSafeInteger);
    };

    // The length property of the from method is 1.
    return function from(arrayLike/*, mapFn, thisArg */) {
      // 1. Let C be the this value.
      var C = this;

      // 2. Let items be ToObject(arrayLike).
      var items = Object(arrayLike);

      // 3. ReturnIfAbrupt(items).
      if (arrayLike == null) {
        throw new TypeError('Array.from requires an array-like object - not null or undefined');
      }

      // 4. If mapfn is undefined, then let mapping be false.
      var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
      var T;
      if (typeof mapFn !== 'undefined') {
        // 5. else
        // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
        if (!isCallable(mapFn)) {
          throw new TypeError('Array.from: when provided, the second argument must be a function');
        }

        // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
        if (arguments.length > 2) {
          T = arguments[2];
        }
      }

      // 10. Let lenValue be Get(items, "length").
      // 11. Let len be ToLength(lenValue).
      var len = toLength(items.length);

      // 13. If IsConstructor(C) is true, then
      // 13. a. Let A be the result of calling the [[Construct]] internal method 
      // of C with an argument list containing the single item len.
      // 14. a. Else, Let A be ArrayCreate(len).
      var A = isCallable(C) ? Object(new C(len)) : new Array(len);

      // 16. Let k be 0.
      var k = 0;
      // 17. Repeat, while k < len… (also steps a - h)
      var kValue;
      while (k < len) {
        kValue = items[k];
        if (mapFn) {
          A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
        } else {
          A[k] = kValue;
        }
        k += 1;
      }
      // 18. Let putStatus be Put(A, "length", len, true).
      A.length = len;
      // 20. Return A.
      return A;
    };
  }());
}

Note that it comes with the following remarks :

This algorithm is exactly the one specified in ECMA-262, 6th edition, assuming Object and TypeError have their original values and that callback.call evaluates to the original value of Function.prototype.call. In addition, since true iterables can not be polyfilled, this implementation does not support generic iterables as defined in the 6th edition of ECMA-262.

That means that there are a few caveats, but it should be more than sufficient for most purposes!

这篇关于在IE(Javascript)中将对象转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆