0

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

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.

T dhanunjay
  • 790
  • 1
  • 13
  • 46
  • You want to put sixth element to the top of the array, am i right? So instead of [1,2,3,4,5,6] you would have [6,1,2,3,4,5] – ZloiGoroh Oct 29 '21 at 05:30
  • Yes right. But want to change array element position without swapping. i.e, 6th element to 1st position. – T dhanunjay Oct 29 '21 at 05:32
  • 2
    Does this answer your question? [Move an array element from one array position to another](https://stackoverflow.com/questions/5306680/move-an-array-element-from-one-array-position-to-another) – aerial Oct 29 '21 at 05:34

2 Answers2

1

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;
    });
},
ZloiGoroh
  • 411
  • 4
  • 11
Forbidden
  • 109
  • 2
  • 10
0

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.length-1]);
arr.splice(arr.length-1);
Amaarockz
  • 4,348
  • 2
  • 9
  • 27