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

255
Vistas
How to improve function that toggles element in map?

There is a function:

toggleSelect(key: string, object: RegistryLayerItemGeneric, selected: boolean) {
    if (selected) {
        let objects = this.state.selectedRegistryObjects.get(key);

        if (objects && object.ObjectId in objects) {
            delete objects[object.ObjectId];
        }

        this.state.selectedRegistryObjects.set(key, {
            ...objects,
        });

        return;
    }

    const objects = {
        ...(this.state.selectedRegistryObjects.get(key) || {}),
        ...{ [object.ObjectId]: object },
    };

    this.state.selectedRegistryObjects.set(key, objects);
}

This function deletes element in map and adds if not exist. How can I improve it? I think this functon complecated for understanding.

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

0

There are a few things I would do here that should help.

  1. Split the function into multiple functions so that each function only does one task. I have begun doing this below

  2. Rename the parameters to be more descriptive of what they contain, i.e. instead of "selected: bool", say what selected is "mapElementExists: bool".

  3. Rename the function to be more descriptive, so that the reader knows what is toggled here

I have begun doing this below

toggleMapElements(key: string, object: RegistryLayerItemGeneric, mapElementExists: boolean) {
    if (mapElementExists) {
        removeMapElement(key: string, object: RegistryLayerItemGeneric);
    } else {
        addMapElement(key: string, object: RegistryLayerItemGeneric)
    }
}

removeMapElement(key: string, object: RegistryLayerItemGeneric) {
    let objects = this.state.selectedRegistryObjects.get(key);

    if (objects && object.ObjectId in objects) {
        delete objects[object.ObjectId];
    }

    this.state.selectedRegistryObjects.set(key, {
        ...objects,
    });

    return;
}

addMapElement(key: string, object: RegistryLayerItemGeneric) {
    const objects = {
        ...(this.state.selectedRegistryObjects.get(key) || {}),
        ...{ [object.ObjectId]: object },
    };

    this.state.selectedRegistryObjects.set(key, objects);
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

That is definitely a complicated function.

Here's a simpler version

toggleSelect(key: string, object: RegistryLayerItemGeneric, selected: boolean) {
  const objects = (this.state.selectedRegistryObjects.get(key) || {});

  if (selected) {
    delete objects[object.ObjectId];  
  } else {
    objects[object.ObjectId] = object;
  }      

  this.state.selectedRegistryObjects.set(key, {...objects});
}

I don't have a typescript environment in front of me, so can't confirm whether it passes all the typescript noise, but this will work from a javascript perspective.

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