說我有這個數組如何手動排序(移動)周圍的數組元素,在反應和/或JavaScript?
arr = ["me", "you", "us", "them"]
我希望能夠當我點擊它, 到每個數組元素拉昇指數,例如當我點擊「他們」的陣列應該像
arr = ["me", "you", "them", "us" ]
我想在理論上使用splice()
它看起來很簡單,但我不能讓我的頭靠近它。這是我的代碼
moveRowUp = (to, frm) => {
const {layout} = this.state
if(to >= layout.length){
let diff = to - layout.length;
while((diff--) + 1){
layout.push(undefined)
}
}
layout.splice(to, 0, layout.splice(to, 1)[0]);
// this.setState({
// layout: layout
// })
}
[如何創建一個最小的,完整的,並且可驗證示例](https://stackoverflow.com/help/mcve) – Andreas
你爲什麼不發佈你試圖實現這個目標的代碼? –
有一個很好的笑@andreas。 – Plankton