There is a function:
JavaScript
x
23
23
1
toggleSelect(key: string, object: RegistryLayerItemGeneric, selected: boolean) {
2
if (selected) {
3
let objects = this.state.selectedRegistryObjects.get(key);
4
5
if (objects && object.ObjectId in objects) {
6
delete objects[object.ObjectId];
7
}
8
9
this.state.selectedRegistryObjects.set(key, {
10
objects,
11
});
12
13
return;
14
}
15
16
const objects = {
17
this.state.selectedRegistryObjects.get(key) || {}), (
18
object.ObjectId]: object }, { [
19
};
20
21
this.state.selectedRegistryObjects.set(key, objects);
22
}
23
This function deletes element in map and adds if not exist. How can I improve it? I think this functon complecated for understanding.
Advertisement
Answer
That is definitely a complicated function.
Here’s a simpler version
JavaScript
1
12
12
1
toggleSelect(key: string, object: RegistryLayerItemGeneric, selected: boolean) {
2
const objects = (this.state.selectedRegistryObjects.get(key) || {});
3
4
if (selected) {
5
delete objects[object.ObjectId];
6
} else {
7
objects[object.ObjectId] = object;
8
}
9
10
this.state.selectedRegistryObjects.set(key, {objects});
11
}
12
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.