Home > Mobile >  How to change array element position without swapping in Vuejs?
How to change array element position without swapping in Vuejs?

Time:10-29

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;
    });
},
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

How to change array element position without swapping in Vuejs?

I have used below logic for swaping elements, Where with the below code. 6th position changed to 1st position. and 1st position swapped with 6th position.

 let tmp = data.groups_with_selected[1];
  data.groups_with_selected[1] = data.groups_with_selected[6];
  data.groups_with_selected[6] = tmp;

But problem here is, I want to change array element position only. But without swapping. How can I proceed i am not sure, tried with many logic.

CodePudding user response:

I guess the code is fine but, it needs tweaks:

  1. You must remove the 6th position via arr.splice(index, 1)
  2. Then insert the 1st postion via arr.splice(index, 0, tmp)

Please see JS Splice

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;
    });
},

CodePudding user response:

If you want to put last element to the top of the array then follow the below approach

Assume that your array is as below

arr=[6,1,2,3,4,5];

then you can do something like

arr.unshift(arr[arr-1]);
arr.splice(arr.length-1);
  • Related