2 回答
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊
可能Array.prototype.slice會(huì)給你一些幫助。
const arr = [1,2,3,4,5,6]
const requiredIndexes = 3
console.log(arr.slice(0, arr.length - requiredIndexes + 1))
TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個(gè)贊
我不是專(zhuān)家,但我認(rèn)為您會(huì)想要迭代到這個(gè)位置。您的動(dòng)態(tài)數(shù)字是隨機(jī)數(shù)嗎?您可能還想克隆陣列并將克隆用作工作陣列。不確定您到底想對(duì)數(shù)組做什么,但您可以從數(shù)組中進(jìn)行切片,并且父數(shù)組不受影響。
function pickposition() {
var randIndex = Math.floor(Math.random() * clone.length);
rand = clone[randIndex];
clone.splice(randIndex,1);
}
添加回答
舉報(bào)
