Notice
Recent Posts
Recent Comments
Link
«   2025/10   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

리액트 정리

[JS] Array() 생성자, Array.prototype.fill() 본문

자바스크립트

[JS] Array() 생성자, Array.prototype.fill()

버그킴 2020. 2. 20. 14:56

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

 

Array() 생성자

Array() 생성자는 새로운 Array 객체를 생성할 때 사용합니다.

developer.mozilla.org

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/fill

 

Array.prototype.fill()

fill() 메서드는 배열의 시작 인덱스부터 끝 인덱스의 이전까지 정적인 값 하나로 채웁니다.

developer.mozilla.org

틱택토 두번째 강좌에 나오고,, 자꾸 나와서 정리해봄 

 

 

 

Array()로 Array 생성

.fill(filling value, start position, end position)로 채움. 

[element0, element1, ..., elementN]

new Array(element0, element1[, ...[, elementN]])
new Array(arrayLength)
const array1 = [1, 2, 3, 4];

// fill with 0 from position 1 until position 3
console.log(Array(array1.length).fill(0, 1, 3));
// [undefined, 0, 0, undefined]

// fill with 5 from position 1
console.log(array1.fill(5, 1));
// expected output: [1, 5, 5, 5]

console.log(array1.fill(6));
// expected output: [6, 6, 6, 6]


console.log(Array(0))
// []
console.log(Array(2))
// [undefined, undefined]

'자바스크립트' 카테고리의 다른 글

[object Object]: What does this mean?  (0) 2021.01.05
[JS] 삼항연산자  (0) 2020.02.10
[JS] Array 메소드 2  (0) 2020.01.30
[JS] Array 메소드 1  (0) 2020.01.30