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.

3
  • 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] Commented Oct 29, 2021 at 5:30
  • Yes right. But want to change array element position without swapping. i.e, 6th element to 1st position. Commented Oct 29, 2021 at 5:32
  • 2
    Does this answer your question? Move an array element from one array position to another Commented Oct 29, 2021 at 5:34

2 Answers 2

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;
    });
},
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Array can have more than 6 elements
Ya the solution will still work

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.