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

73
Vistas
Javascript: How to insert a hole into a sparse array or creating a hole

I'm using sparse arrays to be memory efficient with huge datasets. (and to signal the difference between a hole and a undefined or null entry) adding holes at the end is fine by setting datapoints.lentgh.

datapoints = [10,,,20,,,10,1]

No I face the problem how to insert a hole (say before 20)

datapoint.splice(3,0)

does not work, and I can't find a (easy) method to insert holes. (Think I have to rebuild the array but thought to ask before) Or is there a way to change a "defined" entry into a hole? delete datapoint[4]; creates an undefined entry not a hole :(

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

0

Your sparse array is actually an object. You can delete items and you can add items.

Let's suppose you want to add k holes before position offset. Then:

datapoints = [10,,,20,,,10,1];
function addHoles(offset, k, datapoints) {
    let migration = {};
    for (let key in datapoints) {
        let intKey = parseInt(key);
        if (intKey >= offset) {
            migration[intKey] = datapoints[intKey];
            delete datapoints[intKey];
        }
    }
    for (let key in migration) {
        let intKey = parseInt(key);
        datapoints[intKey + k] = migration[intKey];
    }
    return datapoints;
}
console.log(addHoles(3, 3, datapoints));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Think I could work with

datapoint.splice(3,0,undefined);
delete datapoint[3]

It looks like that I have a hole now ....

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