[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/from [Back]  [Original]

Array.from() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Array.from()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

Array.from() Array .

In this article

console.log(Array.from("foo"));
// Expected output: Array ["f", "o", "o"]

console.log(Array.from([1, 2, 3], (x) => x + x));
// Expected output: Array [2, 4, 6]

Array.from(arrayLike[, mapFn[, thisArg]])

arrayLike

, .

mapFn

, .

thisArg

, this mapFn.

Array

Array.from() :

Array.from() mapFn, map ( ). , Array.from(obj, mapFn, thisArg) Array.from(obj).map(mapFn, thisArg), , . , , , .

length from() 1.

ES2015 , , ; , Array.from Array , Array.

String

js
Array.from("foo");
// ['f', 'o', 'o']

Set

js
var s = new Set(["foo", window]);
Array.from(s);
// ['foo', window]

Map

js
var m = new Map([
  [1, 2],
  [2, 4],
  [4, 8],
]);
Array.from(m);
// [[1, 2], [2, 4], [4, 8]]

(arguments)

js
function f() {
  return Array.from(arguments);
}

f(1, 2, 3);
// [1, 2, 3]

Array.from()

js
//        

//  
Array.from([1, 2, 3], (x) => x + x);
// [2, 4, 6]

//   
Array.from({ length: 5 }, (v, k) => k);
// [0, 1, 2, 3, 4]

Array.from ECMA-262 6- ; . , , Array.from , . , ECMA-262 6- ; , Object TypeError callback.call Function.prototype.call. , , , 6- ECMA-262.

js
//   ECMA-262, 6- , 22.1.2.1
// : https://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from
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);
    };

    //  length  from  1.
    return function from(arrayLike /*, mapFn, thisArg */) {
      // 1.  C   this.
      var C = this;

      // 2.  items  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.  mapfn  undefined,  mapping  false.
      var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
      var T;
      if (typeof mapFn !== "undefined") {
        // 5. 
        // 5. a.   IsCallable(mapfn)  false,   TypeError.
        if (!isCallable(mapFn)) {
          throw new TypeError(
            "Array.from: when provided, the second argument must be a function",
          );
        }

        // 5. b.  thisArg ,  T  thisArg;   T  undefined.
        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      [[Construct]]
      //      C   ,    len.
      // 14. a. ,  A  ArrayCreate(len).
      var A = isCallable(C) ? Object(new C(len)) : new Array(len);

      // 16.  k  0.
      var k = 0;
      // 17.  k < len,  ... (  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.  putStatus  Put(A, "length", len, true).
      A.length = len;
      // 20.  A.
      return A;
    };
  })();
}

Specification
ECMAScript 2027 LanguageSpecification
# sec-array.from


Web Proxy Viewer  |  New URL  |  Original Page