Array.prototype.fill()

So'sCode·2022년 1월 24일
0

Method 정리

목록 보기
5/8
post-thumbnail

📌fill() 메서드란?

  • 배열의 시작 인덱스부터 끝 인덱스의 이전까지 정적인 값 하나로 채운다.
  • 구문 : arr.fill(value[, start[, end]])
    • value : 배열을 채울 값 (필수)
    • start : 시작 인덱스, 기본값은 0 (선택)
    • end : 끝 인덱스, 기본값은 this.length (선택)

💻예시1.

const array1 = [1, 2, 3, 4];

// fill with 0 from position 2 until position 4
console.log(array1.fill(0, 2, 4));
// expected output: [1, 2, 0, 0]

// 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]

💻예시2.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.fill("Kiwi");  // Kiwi,Kiwi,Kiwi,Kiwi

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

profile
이왕하는거미루지말고하자.

0개의 댓글