Array.prototype.fill()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since â¨2015ë 9ìâ©.
Array ì¸ì¤í´ì¤ì fill() ë©ìëë ë°°ì´ì ì¸ë±ì¤ ë²ì ë´ì ìë 모ë ìì를 ì ì ê°ì¼ë¡ ë³ê²½í©ëë¤. ê·¸ë¦¬ê³ ìì ë ë°°ì´ì ë°íí©ëë¤.
ìëí´ ë³´ê¸°
const array1 = [1, 2, 3, 4];
// Fill with 0 from position 2 until position 4
console.log(array1.fill(0, 2, 4));
// Expected output: Array [1, 2, 0, 0]
// Fill with 5 from position 1
console.log(array1.fill(5, 1));
// Expected output: Array [1, 5, 5, 5]
console.log(array1.fill(6));
// Expected output: Array [6, 6, 6, 6]
구문
fill(value)
fill(value, start)
fill(value, start, end)
매ê°ë³ì
value-
ë°°ì´ì ì±ì¸ ê°ì ëë¤. ë°°ì´ì 모ë ììë ì íí ì´ ê°ì´ ë ê²ì ëë¤.
valueê° ê°ì²´ì¸ ê²½ì°, ë°°ì´ì ê° ì¬ë¡¯ì í´ë¹ ê°ì²´ë¥¼ 참조í©ëë¤. startOptional-
ì±ì°ê¸°ë¥¼ ììí 0 ê¸°ë° ì¸ë±ì¤ë¡, ì ìë¡ ë³íë©ëë¤.
- ìì ì¸ë±ì¤ë ë°°ì´ì ëë¶í° ê±°ê¾¸ë¡ ì
ëë¤.
start < 0ì¸ ê²½ì°,start + array.lengthê° ì¬ì©ë©ëë¤. start < -array.lengthëëstartê° ìëµë ê²½ì°,0ì´ ì¬ì©ë©ëë¤.start >= array.lengthì´ë©´, ì무 ì¸ë±ì¤ë ì±ìì§ì§ ììµëë¤.
- ìì ì¸ë±ì¤ë ë°°ì´ì ëë¶í° ê±°ê¾¸ë¡ ì
ëë¤.
endOptional-
ì±ì°ê¸°ë¥¼ ëë¼ 0 ê¸°ë° ì¸ë±ì¤ë¡, ì ìë¡ ë³íë©ëë¤.
fill()ìendê¹ì§ ì±ì°ë©°,endë í¬í¨íì§ ììµëë¤.- ìì ì¸ë±ì¤ë ë°°ì´ì ëë¶í° ê±°ê¾¸ë¡ ì
ëë¤.
end < 0ì¸ ê²½ì°,end + array.lengthê° ì¬ì©ë©ëë¤. end < -array.lengthì´ë©´,0ì´ ì¬ì©ë©ëë¤.end >= array.lengthì´ê±°ëendê° ìëµë ê²½ì°,array.lengthê° ì¬ì©ëì´ ëê¹ì§ 모ë ì¸ë±ì¤ê° ì±ìì§ëë¤.endê° ì ìë¡ ë³íë í,afterë³´ë¤ ìì ìì¹íë©´, ì무 ì¸ë±ì¤ë ì±ìì§ì§ ììµëë¤.
- ìì ì¸ë±ì¤ë ë°°ì´ì ëë¶í° ê±°ê¾¸ë¡ ì
ëë¤.
ë°í ê°
valueë¡ ì±ìì§ ë³ê²½ë ë°°ì´ì
ëë¤.
ì¤ëª
fill() ë©ìëë ë³ê²½ ë©ìëì
ëë¤. ì´ ë©ìëë thisì lengthë ë³ê²½íì§ ìì§ë§, thisì ë´ì©ì ë³ê²½í©ëë¤.
fill() ë©ìëë í¬ì ë°°ì´ì ë¹ ì¬ë¡¯ë valueë¡ ì±ìëë¤.
fill() ë©ìëë ë²ì©ì
ëë¤. this ê°ìë length ìì±ë§ ìì ê²ì¼ë¡ ììí©ëë¤. 문ìì´ë ì ì¬ ë°°ì´ì´ì§ë§, 문ìì´ì ë¶ë³ì´ë¯ë¡ ì´ ë©ìë를 ì ì©í기ìë ì í©íì§ ììµëë¤.
ì°¸ê³ :
ë¹ ë°°ì´(length = 0)ì Array.prototype.fill()ì ì¬ì©íë©´ ë°°ì´ì ìì í ë´ì©ì´ ìì¼ë¯ë¡ ë°°ì´ì´ ìì ëì§ ììµëë¤.
ë°°ì´ì ì ì¸í ë Array.prototype.fill()ì ì¬ì©íë ¤ë©´ ë°°ì´ì 길ì´ê° 0ì´ ìëì§ íì¸íì¸ì.
ìì 를 참조íììì¤.
ìì
>fill() ì¬ì©í기
console.log([1, 2, 3].fill(4)); // [4, 4, 4]
console.log([1, 2, 3].fill(4, 1)); // [1, 4, 4]
console.log([1, 2, 3].fill(4, 1, 2)); // [1, 4, 3]
console.log([1, 2, 3].fill(4, 1, 1)); // [1, 2, 3]
console.log([1, 2, 3].fill(4, 3, 3)); // [1, 2, 3]
console.log([1, 2, 3].fill(4, -3, -2)); // [4, 2, 3]
console.log([1, 2, 3].fill(4, NaN, NaN)); // [1, 2, 3]
console.log([1, 2, 3].fill(4, 3, 5)); // [1, 2, 3]
console.log(Array(3).fill(4)); // [4, 4, 4]
// ë°°ì´ì ê° ì¬ë¡¯ì´ 참조íë ë¨ì¼ ê°ì²´
const arr = Array(3).fill({}); // [{}, {}, {}]
arr[0].hi = "hi"; // [{ hi: "hi" }, { hi: "hi" }, { hi: "hi" }]
fill()ì ì¬ì©íì¬ ì ì²´ 1 íë ¬ë¡ ë§ë¤ê¸°
ì´ ìì ë Octave ëë MATLABì ones() í¨ìì ê°ì´ ì ì²´ 1 íë ¬ì ë§ëë ë°©ë²ì ë³´ì¬ì¤ëë¤.
const arr = new Array(3);
for (let i = 0; i < arr.length; i++) {
arr[i] = new Array(4).fill(1); // í¬ê¸°ê° 4ì´ê³ , 1ë¡ ì±ìì§ ë°°ì´ ìì±
}
arr[0][0] = 10;
console.log(arr[0][0]); // 10
console.log(arr[1][0]); // 1
console.log(arr[2][0]); // 1
fill()ì ì¬ì©íì¬ ë¹ ë°°ì´ ì±ì°ê¸°
ì´ ìì ë 모ë ìì를 í¹ì ê°ì¼ë¡ ì¤ì íì¬ ë°°ì´ì ì±ì°ë ë°©ë²ì ë³´ì¬ì¤ëë¤. end 매ê°ë³ìë ì§ì í íìê° ììµëë¤.
const tempGirls = Array(5).fill("girl", 0);
ì´ ë°°ì´ì ì²ìì ì¸ë±ì¤ê° í ë¹ëì§ ìì í¬ì ë°°ì´ì´ììµëë¤. ê·¸ëë fill()ì ì¬ì í ì´ ë°°ì´ì ì±ì¸ ì ììµëë¤.
ë°°ì´ì´ ìë ê°ì²´ìì fill() í¸ì¶í기
fill() ë©ìëë thisì length ìì±ì ì½ê³ startë¶í° endê¹ì§ ê° ì ì í¤ ìì± ê°ì ì¤ì í©ëë¤.
const arrayLike = { length: 2 };
console.log(Array.prototype.fill.call(arrayLike, 1));
// { '0': 1, '1': 1, length: 2 }
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2026 Language Specification > # sec-array.prototype.fill > |