.fill(value)

Description

The fill method replaces all elements in an array with a specified value.

Parameters

  • value: The value to fill the array with. Can be any valid OSL type.

Usage On Arrays

// Fill array with a string
arr = (1 to 5).fill("hello")
log arr  // ["hello", "hello", "hello", "hello", "hello"]

// Fill array with a number
numbers = (1 to 3).fill(42)
log numbers  // [42, 42, 42]

// Fill with other types
booleans = (1 to 4).fill(true)  // [true, true, true, true]
objects = (1 to 2).fill({x: 10})  // [{x: 10}, {x: 10}]

// Common use case: Initialize array with default values
scores = (1 to 10).fill(0)  // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Important Notes

  • The original array is modified

  • Can fill with any OSL data type

  • Often used with the range operator to create arrays of a specific size

  • All elements reference the same value when filling with objects

Last updated

Was this helpful?