shift
從數組的開頭移除指定數量的元素,並返回被移除的元素。
基礎用法
typescript
import { shift } from 'radash'
const numbers = [1, 2, 3, 4, 5]
const [removed, remaining] = shift(numbers, 2)
// removed: [1, 2]
// remaining: [3, 4, 5]
語法
typescript
function shift<T>(
array: readonly T[],
count?: number
): [T[], T[]]
參數
array
(readonly T[]): 要操作的數組count
(number, 可選): 要移除的元素數量,默認為1
返回值
返回一個元組,包含兩個數組:
- 第一個數組:被移除的元素
- 第二個數組:剩余的元素
示例
移除單個元素
typescript
import { shift } from 'radash'
const fruits = ['apple', 'banana', 'cherry', 'date']
const [removed, remaining] = shift(fruits)
// removed: ['apple']
// remaining: ['banana', 'cherry', 'date']
移除多個元素
typescript
import { shift } from 'radash'
const numbers = [1, 2, 3, 4, 5, 6, 7, 8]
const [removed, remaining] = shift(numbers, 3)
// removed: [1, 2, 3]
// remaining: [4, 5, 6, 7, 8]
移除所有元素
typescript
import { shift } from 'radash'
const items = ['a', 'b', 'c']
const [removed, remaining] = shift(items, 3)
// removed: ['a', 'b', 'c']
// remaining: []
移除超過數組長度的元素
typescript
import { shift } from 'radash'
const items = ['a', 'b', 'c']
const [removed, remaining] = shift(items, 5)
// removed: ['a', 'b', 'c']
// remaining: []
處理空數組
typescript
import { shift } from 'radash'
const emptyArray: number[] = []
const [removed, remaining] = shift(emptyArray, 2)
// removed: []
// remaining: []
處理對象數組
typescript
import { shift } from 'radash'
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' },
{ id: 4, name: 'Diana' }
]
const [removedUsers, remainingUsers] = shift(users, 2)
// removedUsers: [
// { id: 1, name: 'Alice' },
// { id: 2, name: 'Bob' }
// ]
// remainingUsers: [
// { id: 3, name: 'Charlie' },
// { id: 4, name: 'Diana' }
// ]
在隊列處理中使用
typescript
import { shift } from 'radash'
class Queue<T> {
private items: T[] = []
enqueue(item: T) {
this.items.push(item)
}
dequeue(count: number = 1): T[] {
const [removed, remaining] = shift(this.items, count)
this.items = remaining
return removed
}
peek(): T | undefined {
return this.items[0]
}
size(): number {
return this.items.length
}
}
const queue = new Queue<number>()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
const dequeued = queue.dequeue(2)
console.log(dequeued) // [1, 2]
console.log(queue.size()) // 1
處理分頁數據
typescript
import { shift } from 'radash'
function processBatch<T>(items: T[], batchSize: number): T[][] {
const batches: T[][] = []
let remaining = [...items]
while (remaining.length > 0) {
const [batch, newRemaining] = shift(remaining, batchSize)
batches.push(batch)
remaining = newRemaining
}
return batches
}
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const batches = processBatch(data, 3)
console.log(batches)
// [
// [1, 2, 3],
// [4, 5, 6],
// [7, 8, 9],
// [10]
// ]
處理時間序列數據
typescript
import { shift } from 'radash'
interface TimeSeriesPoint {
timestamp: number
value: number
}
const timeSeriesData: TimeSeriesPoint[] = [
{ timestamp: 1000, value: 10 },
{ timestamp: 2000, value: 20 },
{ timestamp: 3000, value: 30 },
{ timestamp: 4000, value: 40 },
{ timestamp: 5000, value: 50 }
]
// 移除前兩個數據點
const [removedPoints, remainingPoints] = shift(timeSeriesData, 2)
console.log('Removed:', removedPoints)
// [
// { timestamp: 1000, value: 10 },
// { timestamp: 2000, value: 20 }
// ]
console.log('Remaining:', remainingPoints)
// [
// { timestamp: 3000, value: 30 },
// { timestamp: 4000, value: 40 },
// { timestamp: 5000, value: 50 }
// ]
處理字符串數組
typescript
import { shift } from 'radash'
const words = ['hello', 'world', 'javascript', 'typescript', 'react']
const [removedWords, remainingWords] = shift(words, 2)
// removedWords: ['hello', 'world']
// remainingWords: ['javascript', 'typescript', 'react']
// 將移除的單詞連接成句子
const sentence = removedWords.join(' ')
console.log(sentence) // 'hello world'
處理數字數組
typescript
import { shift } from 'radash'
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// 移除前5個數字
const [removedNumbers, remainingNumbers] = shift(numbers, 5)
console.log('Removed numbers:', removedNumbers) // [1, 2, 3, 4, 5]
console.log('Remaining numbers:', remainingNumbers) // [6, 7, 8, 9, 10]
// 計算移除數字的總和
const sum = removedNumbers.reduce((acc, num) => acc + num, 0)
console.log('Sum of removed numbers:', sum) // 15
處理混合類型數組
typescript
import { shift } from 'radash'
const mixedArray = [1, 'hello', true, { key: 'value' }, [1, 2, 3]]
const [removed, remaining] = shift(mixedArray, 3)
// removed: [1, 'hello', true]
// remaining: [{ key: 'value' }, [1, 2, 3]]
注意事項
- 保持原數組不變:
shift
不會修改原數組,而是返回新的數組 - 返回元組: 返回一個包含兩個數組的元組
- 邊界處理: 如果count超過數組長度,會移除所有元素
- 性能: 時間復雜度為 O(n),其中 n 是數組長度
- 空數組: 對空數組調用會返回兩個空數組
與其他方法的區別
Array.prototype.shift()
: 只移除一個元素並返回該元素shift()
: radash提供的更靈活的版本,可以移除多個元素slice()
: 返回子數組,不修改原數組splice()
: 會修改原數組
實際應用場景
- 隊列處理: 實現先進先出的隊列操作
- 批處理: 分批處理大量數據
- 時間序列: 處理時間序列數據的滑動窗口
- 緩存管理: 移除緩存中的舊數據
- 流處理: 處理數據流中的元素