[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/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 9.

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]

js
Array.from(arrayLike)
Array.from(arrayLike, mapFn)
Array.from(arrayLike, mapFn, thisArg)

arrayLike

.

mapFnOptional

. , mapFn . .

element

.

index

.

thisArgOptional

mapFn this .

Array .

Array.from() Array .

( , ) Object.keys(), Object.values(), Object.entries() . Array.fromAsync() .

Array.from() . arrayLike , undefined .

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

: , . Array.from() TypedArray.from() .

Array.from() . , Array from() , from() Array . this . arrayLike , length . length . this , Array .

String

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

Set

js
const set = new Set(["foo", "bar", "baz", "foo"]);
Array.from(set);
// [ "foo", "bar", "baz" ]

Map

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

const mapper = new Map([
  ["1", "a"],
  ["2", "b"],
]);
Array.from(mapper.values());
// ['a', 'b'];

Array.from(mapper.keys());
// ['1', '2'];

NodeList

js
// DOM     
const images = document.querySelectorAll("img");
const sources = Array.from(images, (image) => image.src);
const insecureSources = sources.filter((link) => link.startsWith("http://"));

(arguments)

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

f(1, 2, 3);

// [ 1, 2, 3 ]

Array.from()

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

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

(range)

js
//    (Clojure, PHP  "range" )
const range = (start, stop, step) =>
  Array.from({ length: (stop - start) / step + 1 }, (_, i) => start + i * step);

// 0..4   
range(0, 4, 1);
// [0, 1, 2, 3, 4]

//   1...10   
range(1, 10, 2);
// [1, 3, 5, 7, 9]

//     `Array.from`  
range("A".charCodeAt(0), "Z".charCodeAt(0), 1).map((x) =>
  String.fromCharCode(x),
);
// ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

from()

from() length .

js
function NotArray(len) {
  console.log("NotArray called with length", len);
}

//  
console.log(Array.from.call(NotArray, new Set(["foo", "bar", "baz"])));
// NotArray length undefined  .
// NotArray { '0': 'foo', '1': 'bar', '2': 'baz', length: 3 }

//  
console.log(Array.from.call(NotArray, { length: 1, 0: "foo" }));
// NotArray length 1  .
// NotArray { '0': 'foo', length: 1 }

this , Array .

js
console.log(Array.from.call({}, { length: 1, 0: "foo" })); // [ 'foo' ]

Specification
ECMAScript 2027 LanguageSpecification
# sec-array.from


Web Proxy Viewer  |  New URL  |  Original Page