Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

253
Vistas
How to change array element position without swapping in Vuejs?

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.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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;
    });
},
about 4 years ago · Juan Pablo Isaza Denunciar

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);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda