這個問題在這里已經有了答案: 為什么切片不起作用? (2 個回答) 4 小時前關閉。
我已經使用 slice 一段時間了,沒有問題,我的問題在于 .slice() 不切片。代碼:(為什么會破壞代碼??)
let data
data = await Models.find().sort([['points', 'descending']])
console.log(data.length) // output: 179
data.slice(0,5)
console.log(data.length) // output: 179
以下是資料回傳的內容:
{
points: 100,
commandsUsed: 0,
tipDifficulty: 'Not Set.',
mapDifficulty: 'Not Set.',
stateDifficulty: 'Not Set.',
gameType: 'Not Set.',
items: [],
_id: 623d29b01fde8a4b347eadb7,
難度:'未設定',
用戶ID:'1234567890 ',服務器ID
:'1234567890',
__v:0},
一遍又一遍地重復(179次)
I've tried splice(), .filter() but splice should work, it always has before(discord js 12).
uj5u.com熱心網友回復:
data.slice(0,5)不修改輸入陣列data。相反,它回傳一個新陣列,其中包含所選元素的淺拷貝:
let results = data.slice(0,5);
console.log(results.length); // 5 (assuming data contained more than 5 elements)
正確使用,.splice()將通過洗掉元素或插入元素來修改輸入陣列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/456299.html
上一篇:反應條件渲染子
