const data = response.data
console.log(data)
const temp = data.groups_with_selected.splice(7,1)(21,3)[0]
data.groups_with_selected.splice(2,21,0,temp)
如何拼接多個陣列元素并相應插入?
在上面的代碼中,來自回應。我正在拼接陣列元素位置并將它們放在某個位置。
我想拼接(第7位至第2位)和(第21位至第3位)
但在這個程序中,我無法重新安排訂單。
uj5u.com熱心網友回復:
好,
splice(7,1)(21,3)
此代碼將導致錯誤。由于 Array.prototpy.slice 回傳一個新陣列。
如果你這樣做也是一樣的:
const a = [1,2,3]
const b = a.splice(1,1);
b(2,1) // b.splice(...) is not a function
編輯:也許有一個更快/更好的解決方案,但是......你可以讓它更一般,但只針對你的情況:
const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21];
const first = array[7];
const second = array[21];
// Add elements on certain indices in array (second and third)
array.splice(2, 0, first, second)
// Remove first from the array (index is 7 2 because we added 2 elements)
array.splice(9, 1)
// Remove 21 from the array (index is 22 - 1 because we added 2 elements and removed 1, not: number 21 is on index 22)
array.splice(21, 1);
uj5u.com熱心網友回復:
data不應該是 a,const因為值正在更新。Splice也只能一次在一個陣列上呼叫。如果您需要在多個陣列上呼叫它,請創建一個回圈或迭代它們。
如果你想將第 7 個位置的當前值注入到第 2 個位置......你需要做一些類似的事情......
array.splice(2, 0, array[7])
uj5u.com熱心網友回復:
單程 :
arr = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
arr.splice(2, 0, arr[7], arr[21])
arr.splice(9, 1)
arr.splice(22, 1)
console.log(JSON.stringify(arr))
//[0,1,7,21,2,3,4,5,6,8,9,10,11,12,13,14,15,16,17,18,19,20,]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/344808.html
標籤:javascript Vue.js 公理
