dogo_ day

JavaScript Array.from 본문

IT/JavaScript_

JavaScript Array.from

dogo_ 2023. 5. 1. 15:39
Array.from()
 메서드는 유사 배열 객체(array-like object)나 반복 가능한 객체(iterable object)를 얕게 복사해 새로운 Array
 객체를 만듭니다.

출처 : https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/from

 

// Using an arrow function as the map function to
// manipulate the elements
Array.from([1, 2, 3], x => x + x);
// [2, 4, 6]

// Generate a sequence of numbers
// Since the array is initialized with `undefined` on each position,
// the value of `v` below will be `undefined`
Array.from({length: 5}, (v, i) => i);
// [0, 1, 2, 3, 4]

// from() 메서드의 length 속성은 1입니다.

🌈 0부터 999까지의 정수 30000개를 담은 배열 생성

let arr = Array.from({length: 30000}, () => Math.floor(Math.random() * 1000));

참고자료

패스트캠퍼스 - JavaScript 코딩테스트 131개 예재 & CS지식으로 끝내기