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

161
Vistas
How to splice multiple array elements and insert accordingly?

const data = response.data
console.log(data)
const temp = data.ssps_with_scated.splice(5, 1)(1, 3)[0]
data.ps_with_ed.splice(2, 1, 0, temp) 

i am trying to achieve finally i got it. But issue is, i cant expect the array value same all the time. So i have decided to re-arrange the array values based on the ID.

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

0

Well,

splice(7,1)(21,3)

This code will cause an error. Since Array.prototpy.slice returns a new array.
It would be the same if you would do this:

const a = [1,2,3]
const b = a.splice(1,1);
b(2,1) // b.splice(...) is not a function

EDITED: Maybe there is a faster/better solution but... You can make it more general but for your case only:

const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21];
const first = array[7];
const second = array[21];

// Add elements on certain indices in array (second and third)
array.splice(2, 0, first, second)

// Remove first from the array (index is 7 + 2 because we added 2 elements)
array.splice(9, 1)

// Remove 21 from the array (index is 22 - 1 because we added 2 elements and removed 1, not: number 21 is on index 22)
array.splice(21, 1);
about 4 years ago · Juan Pablo Isaza Denunciar

0

data shouldn't be a const since the value is getting updated. Splice can also only be called on one array at a time. If you need to call it on multiple arrays, create a loop or iterate over them.

If you want to inject the current value of the 7th position into the 2nd position... you'd need to do something like...

array.splice(2, 0, array[7])

about 4 years ago · Juan Pablo Isaza Denunciar

0

One way :

 arr = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
 
arr.splice(2, 0, arr[7], arr[21])
 arr.splice(9, 1)
 arr.splice(22, 1)
 console.log(JSON.stringify(arr))
 //[0,1,7,21,2,3,4,5,6,8,9,10,11,12,13,14,15,16,17,18,19,20,] 

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