Baseline Widely available
fill()
㯠Array
ã¤ã³ã¹ã¿ã³ã¹ã®ã¡ã½ããã§ãã¤ã³ããã¯ã¹ã®ç¯å²å
ã«ããé
åã®ãã¹ã¦ã®è¦ç´ ãä¸å®ã®å¤ã«å¤æ´ãã¾ããããã¯å¤æ´ããé
åãè¿ãã¾ãã
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
ããªãã¸ã§ã¯ãã§ããã°ãé
åã®ããããã®è¦ç´ ã¯ãã®ãªãã¸ã§ã¯ããåç
§ãã¾ãã
start
çç¥å¯
åãå§ããä½ç½®ã®ã¼ãããå§ã¾ãã¤ã³ããã¯ã¹ã§ãæ´æ°ã«å¤æããã¾ãã
start < 0
ã®å ´åã start + array.length
ã使ç¨ããã¾ããstart < -array.length
ã¾ã㯠start
ãçç¥ãããå ´å㯠0
ã使ç¨ããã¾ããstart >= array.length
ã®å ´åãåããããã¤ã³ããã¯ã¹ã¯ããã¾ãããend
çç¥å¯
åãçµããä½ç½®ã®ã¼ãããå§ã¾ãã¤ã³ããã¯ã¹ã§ãæ´æ°ã«å¤æããã¾ãã fill()
㯠end
ãå«ã¾ãããã®ç´åã¾ã§ãåãã¾ãã
end < 0
ã®å ´åã end + array.length
ã使ç¨ããã¾ããend < -array.length
ã®å ´å㯠0
ã使ç¨ããã¾ããend >= array.length
ã¾ã㯠end
ãçç¥ãããå ´åãvalue
ã§åãããã¦å¤æ´ãããé
åã§ãã
fill()
ã¡ã½ããã¯å¤æ´ã¡ã½ããã§ãããã㯠this
ã®é·ãã¯å¤æ´ãã¾ãããã this
ã®ã³ã³ãã³ãã¯å¤æ´ãã¾ãã
fill()
ã¡ã½ããã¯çé
åã®ç©ºã®ã¹ããããã value
ã§åãã¾ãã
every()
ã¡ã½ããã¯æ±ç¨çã§ãããã®ã¡ã½ãã㯠this
ã®å¤ã« length
ããããã£ã¨æ´æ°ã®ãã¼ãæã£ãããããã£ããããã¨ã ããæ±ãã¾ããæååãé
å風ã®ãã®ã§ãããæååã¯ä¸å¤ãªã®ã§ããã®ã¡ã½ãããé©ç¨ããã®ã¯é©ãã¦ãã¾ããã
ã¡ã¢: Array.prototype.fill()
ã空ã®é
åã«å¯¾ãã¦ä½¿ç¨ããã¨ãé
åã«å¤æ´ãããã®ããªãã®ã§ä½ã夿´ããã¾ããã é
åã宣è¨ããéã« Array.prototype.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()
ã¡ã½ãã㯠this
ã® length
ããããã£ãèªã¿åãã start
ãã end
ã¾ã§ã®åæ´æ°ãã¼ã®ããããã£ã®å¤ãè¨å®ãã¾ãã
const arrayLike = { length: 2 };
console.log(Array.prototype.fill.call(arrayLike, 1));
// { '0': 1, '1': 1, length: 2 }
仿§æ¸ ãã©ã¦ã¶ã¼ã®äºææ§ é¢é£æ
å ±
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4