Skip to content

@pmun/utils / shuffle

Function: shuffle()

ts
function shuffle<T>(array): T[];

随机打乱数组元素顺序

Type Parameters

T

T

Parameters

array

T[]

原始数组

Returns

T[]

打乱后的新数组,不会修改原始数组

Example

ts
const numbers = [1, 2, 3, 4, 5]
const shuffled = shuffle(numbers)
// 可能的结果: [3, 1, 5, 2, 4]
console.log(numbers) // 原数组不变: [1, 2, 3, 4, 5]