list
List operation function.
Create an array of specified length and fill it with the specified value.
Basic Usage
typescript
import { list } from 'radash'
// Create an array of length 5, filled with 0
const zeros = list(5, 0)
// [0, 0, 0, 0, 0]
// Create an array of length 3, filled with 'hello'
const greetings = list(3, 'hello')
// ['hello', 'hello', 'hello']
Syntax
typescript
function list<T>(
length: number,
value: T
): T[]
Parameters
length
(number): The length of the array to createvalue
(T): The value to fill the array with
Return Value
Returns an array of specified length with all elements equal to the specified value.
Examples
Creating Number Arrays
typescript
import { list } from 'radash'
// Create 5 ones
const ones = list(5, 1)
// [1, 1, 1, 1, 1]
// Create 10 zeros
const zeros = list(10, 0)
// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
// Create 3 negative ones
const negatives = list(3, -1)
// [-1, -1, -1]
Creating String Arrays
typescript
import { list } from 'radash'
// Create 4 empty strings
const emptyStrings = list(4, '')
// ['', '', '', '']
// Create 3 'placeholder'
const placeholders = list(3, 'placeholder')
// ['placeholder', 'placeholder', 'placeholder']
// Create 2 '*'
const stars = list(2, '*')
// ['*', '*']
Creating Object Arrays
typescript
import { list } from 'radash'
// Create 3 empty objects
const emptyObjects = list(3, {})
// [{}, {}, {}]
// Create 2 default user objects
const defaultUser = { id: 0, name: '', age: 0 }
const users = list(2, defaultUser)
// [
// { id: 0, name: '', age: 0 },
// { id: 0, name: '', age: 0 }
// ]
Creating Boolean Arrays
typescript
import { list } from 'radash'
// Create 5 trues
const trues = list(5, true)
// [true, true, true, true, true]
// Create 3 falses
const falses = list(3, false)
// [false, false, false]
Creating Array Arrays
typescript
import { list } from 'radash'
// Create 3 empty arrays
const emptyArrays = list(3, [])
// [[], [], []]
// Create 2 default arrays
const defaultArray = [1, 2, 3]
const arrays = list(2, defaultArray)
// [[1, 2, 3], [1, 2, 3]]
Creating Null or Undefined Arrays
typescript
import { list } from 'radash'
// Create 4 nulls
const nulls = list(4, null)
// [null, null, null, null]
// Create 3 undefineds
const undefineds = list(3, undefined)
// [undefined, undefined, undefined]
Combining with map
typescript
import { list } from 'radash'
// Create index array
const indices = list(5, 0).map((_, index) => index)
// [0, 1, 2, 3, 4]
// Create even number array
const evens = list(5, 0).map((_, index) => index * 2)
// [0, 2, 4, 6, 8]
// Create letter array
const letters = list(26, '').map((_, index) => String.fromCharCode(97 + index))
// ['a', 'b', 'c', ..., 'z']
Notes
- Shallow copy: If objects or arrays are passed in, all elements will reference the same object
- Length limit: Length must be a non-negative integer
- Performance: Time complexity is O(n), where n is the array length
- Memory: Will create an array of specified length, pay attention to memory usage
Differences from Other Methods
Array(n).fill(value)
: Native method, same functionalitylist()
: More concise API provided by radashArray.from({length: n}, () => value)
: More flexible but more complex
Practical Application Scenarios
- Initialize arrays: Create fixed-length arrays for subsequent operations
- Placeholders: Create placeholder elements for forms, lists, etc.
- Test data: Create repetitive data for testing
- Buffers: Create fixed-size buffers
- Matrix initialization: Initialize rows or columns of 2D arrays