TypedArray.prototype.fill()
Baseline
広く利用可能
この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2016年9月以降、すべてのブラウザーで利用可能です。
fill() は TypedArray インスタンスのメソッドで、型付き配列のある範囲のインデックスにあるすべての要素を固定値で埋めます。これは変更された型付き配列を返します。このメソッドのアルゴリズムは Array.prototype.fill() と同じです。
試してみましょう
const uint8 = new Uint8Array([0, 0, 0, 0]);
// 値、開始位置、終了位置
uint8.fill(4, 1, 3);
console.log(uint8);
// 予想される結果: 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]
仕様書
| 仕様書 |
|---|
| ECMAScript® 2027 Language Specification> # sec-%typedarray%.prototype.fill> |
ブラウザーの互換性
ブラウザー互換性一覧表を表示するには、JavaScript を有効にしてください。