【发布时间】:2021-12-14 05:47: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 位。第 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;
但这里的问题是,我只想更改数组元素的位置。但是不用换。我不确定如何继续,尝试了很多逻辑。
【问题讨论】:
-
你想把第六个元素放在数组的顶部,对吗?因此,您将拥有 [6,1,2,3,4,5] 而不是 [1,2,3,4,5,6]
-
是的,没错。但是想在不交换的情况下更改数组元素的位置。即,第 6 个元素到第 1 个位置。
标签: javascript vue.js axios