| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/from | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..
Array.from() Array .
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]])
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.
StringArray.from("foo");
// ['f', 'o', 'o']
Setvar s = new Set(["foo", window]);
Array.from(s);
// ['foo', window]
Mapvar m = new Map([
[1, 2],
[2, 4],
[4, 8],
]);
Array.from(m);
// [[1, 2], [2, 4], [4, 8]]
function f() {
return Array.from(arguments);
}
f(1, 2, 3);
// [1, 2, 3]
Array.from()//
//
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.
// 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 |
This page was last modified on 29 . 2026 . by MDN contributors.
ArrayArray.prototype.at()Array.prototype.concat()Array.prototype.copyWithin()Array.prototype.entries()Array.prototype.every()Array.prototype.fill()Array.prototype.filter()Array.prototype.find()Array.prototype.findIndex()findLast()findLastIndex()Array.prototype.flat()Array.prototype.flatMap()Array.prototype.forEach()Array.prototype.includes()Array.prototype.indexOf()Array.prototype.join()Array.prototype.keys()Array.prototype.lastIndexOf()Array.prototype.map()Array.prototype.pop()Array.prototype.push()Array.prototype.reduce()Array.prototype.reduceRight()Array.prototype.reverse()Array.prototype.shift()Array.prototype.slice()Array.prototype.some()Array.prototype.sort()Array.prototype.splice()Array.prototype.toLocaleString()Array.prototype.toReversed()Array.prototype.toSorted()toSpliced()Array.prototype.toString()Array.prototype.unshift()Array.prototype.values()Array.prototype.with()Array.prototype[@@iterator]()Object/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |