このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。
TypedArray.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 2016年9月.
fill() はTypedArray インスタンスのメソッドで、型付き配列のある範囲のインデックスにあるすべての要素を固定値で埋めます。これは変更された型付き配列を返します。このメソッドのアルゴリズムはArray.prototype.fill() と同じです。
In this article
試してみましょう
const uint8 = new Uint8Array([0, 0, 0, 0]);// Value, start position, end positionuint8.fill(4, 1, 3);console.log(uint8);// Expected output: Uint8Array [0, 4, 4, 0]構文
js
fill(value)fill(value, start)fill(value, start, end)引数
返値
変更された配列です。
解説
詳細については、Array.prototype.fill() をご覧ください。このメソッドは汎用的ではなく、型付き配列インスタンスに対してのみ呼び出すことができます。
例
>fill() の使用
js
new Uint8Array([1, 2, 3]).fill(4); // Uint8Array [4, 4, 4]new Uint8Array([1, 2, 3]).fill(4, 1); // Uint8Array [1, 4, 4]new Uint8Array([1, 2, 3]).fill(4, 1, 2); // Uint8Array [1, 4, 3]new Uint8Array([1, 2, 3]).fill(4, 1, 1); // Uint8Array [1, 2, 3]new Uint8Array([1, 2, 3]).fill(4, -3, -2); // Uint8Array [4, 2, 3]仕様書
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-%typedarray%.prototype.fill> |