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

217
Vistas
Removing subarray from array with splice

I am using splice method to remove a subarray from an array with the following code:


    for(let i = 0; i < cid.length; i++)
    {
      if(coinsValue[cid[i][0]] < troco && cid[i][1] > 0)
          available += cid[i][1]
      if(coinsValue[cid[i][0]] > troco)
          cid[i].splice(0)
    }


The output is :

[ [ 'PENNY', 1.01 ],
  [ 'NICKEL', 2.05 ],
  [ 'DIME', 3.1 ],
  [ 'QUARTER', 4.25 ],
  [ 'ONE', 90 ],
  [ 'FIVE', 55 ],
  [ 'TEN', 20 ],
  [ 'TWENTY', 60 ],
  [] /// removed elements from this array, but not the array itself ]

As you can see the elements that fit the condition are removed and I'm still left with an empty array. However I want to remove it completely!

Any help?

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

0

You're removing items from a sub array, not from the array itself.

To remove item from the top array you should do like this:

cid.splice(i, 1) instead of cid[i].splice(0)

for(let i = 0; i < cid.length; i++) {
    if (coinsValue[cid[i][0]] < troco && cid[i][1] > 0) {
        available += cid[i][1];
    } else if (coinsValue[cid[i][0]] > troco) {
        cid.splice(i, 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