Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!