這個問題在這里已經有了答案: 將陣列元素從一個陣列位置移動到另一個 34 個答案 昨天關閉。
groupsCall(callType, params) {
let url = `/....../`,
errorMessage = `could not load the items`;
url =
callType === "get" ? url "selected_groups" : url "update_groups";
axiosConfig[callType](url, params)
.then((response) => {
const data = response.data;
console.log("hittttt", data.groups_with_selected);
let tmp = data.groups_with_selected[1];
data.groups_with_selected[1] = data.groups_with_selected[6];
data.groups_with_selected[6] = tmp;
if (isObject(data)) {
this.setGroupsData(data);
this.getTotalCount();
errorMessage = "";
this.setUpdating(data);
}
this.errorMessage = errorMessage;
})
.catch((error) => {
errorMessage = `${errorMessage}. ${error}`;
this.errorMessage = errorMessage;
this.updating = false;
});
},
如何在不交換Vuejs的情況下更改陣列元素位置?
我使用以下邏輯交換元素,其中包含以下代碼。第 6 位更改為第 1 位。第一個位置與第 6 個位置互換。
let tmp = data.groups_with_selected[1];
data.groups_with_selected[1] = data.groups_with_selected[6];
data.groups_with_selected[6] = tmp;
但這里的問題是,我只想改變陣列元素的位置。但沒有交換。我不確定如何繼續,嘗試了很多邏輯。
uj5u.com熱心網友回復:
我想代碼很好,但是,它需要調整:
- 您必須通過洗掉第 6 個位置
arr.splice(index, 1) - 然后通過插入第一個位置
arr.splice(index, 0, tmp)
請看JS拼接
groupsCall(callType, params) {
let url = `/api/v3/campaigns/${this.campaignId}/`,
errorMessage = `could not load the filters`;
url =
callType === "get" ? url "selected_groups" : url "update_groups";
axiosConfig[callType](url, params)
.then((response) => {
const data = response.data;
console.log("hittttt", data.groups_with_selected);
let tmp = data.groups_with_selected[6];
// Remove the 6th position
data.groups_with_selected.splice(6, 1)
// Insert tmp
data.groups_with_selected.splice(1, 0, tmp)
// This code is not needed
// data.groups_with_selected[6] = tmp;
if (isObject(data)) {
this.setGroupsData(data);
this.getTotalCount();
errorMessage = "";
this.setUpdating(data);
}
this.errorMessage = errorMessage;
})
.catch((error) => {
errorMessage = `${errorMessage}. ${error}`;
this.errorMessage = errorMessage;
this.updating = false;
});
},
uj5u.com熱心網友回復:
如果要將最后一個元素放在陣列的頂部,請遵循以下方法
假設你的陣列如下
arr=[6,1,2,3,4,5];
然后你可以做類似的事情
arr.unshift(arr[arr.length-1]);
arr.splice(arr.length-1);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/342603.html
標籤:javascript Vue.js 公理
下一篇:單擊時更改v-chip的顏色
