Get to know MDN better
此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年9月.
Array.from() 方法會從類陣列(array-like)或是可迭代(iterable)物件建立一個新的 Array 實體。
將類陣列或可迭代物件轉換成陣列
mapFn 選擇性Map 函式走訪陣列中的每一個元素。
thisArg 選擇性mapFn 函式執行時的 this 對象。
一個新的 Array 實體。
Array.from() 讓你從這些物件建立陣列:
Array.from() 有個可選用的參數 mapFn,它允許你在建立出新的陣列實體之後,可以接著對陣列(或是其子類別物件)中的每一個元素執行 map 函式。更清楚地說,Array.from(obj, mapFn, thisArg) 跟 Array.from(obj).map(mapFn, thisArg) 的結果是一樣的,除非所建立的不是一個可用的中介陣列(intermediate array)。這對於某些陣列的子類別來說就很重要,例如型別陣列,因為中介陣列必須要把內容值做一番截頭去尾的操作來讓它們變成適合的物件型態。
from() 方法的 length 屬性值為 1。
在 ES2015,類別語法允許原生內建的物件以及使用者自定義的物件可以被子類別化(sub-classing);因此,靜態方法像是 Array.from,是「繼承」了 Array 的子類別後,然後建立新的子類別的實體,而不是建立 Array 本身。
Array.from 在 ECMA-262 標準第六版(ES2015)被加入;在某些實作可能尚未被支援。你可以將下面的程式碼插入到妳的 script 的最前面,如果你使用的工作環境不具有原生支援 Array.from 的能力。這個演算法根據 ECMA-262 第六版中的規範實現,假定 Object 及 TypeError 它們本身已具有值且 callback.call 對應到原本 Function.prototype.call 的值。除此之外,因為 Polyfill 無法實現真正的迭代,這個實作不支援 ECMA-262 第六版中所定義的泛型迭代。
| ECMAScript® 2027 Language Specification # sec-array.from |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2025年7月14日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.