I'm currently trying to build an addOrUpdate function
.
Unfortunately I failed several times.
Basically, I have an array of objects. Each object in turn has an array of strings
values
(and a key valueGroup
).
First you have to check if the object exists, if not a new one should be added. If the object already exists, I want to check whether the value I want to insert is already in the object's string array. If not add it. Is it there delete it!
I first tried it functionally via mapper, but I failed completely because I got a double array. Of course, it would be best to solve it completely functionally. Is it possible?
Currently there is no update of the objects. A new object is always inserted, again with an array string containing a new value.
I also find the current code too complicated
I am using Typescript
type SelectedValues = {
valueGroup: string;
values: string[];
};
function updateSelectedValues(selectedValues: SelectedValues[], valueGroup: string, value: string): SelectedValues[] {
// If array empty add first object
if (selectedValues.length === 0) {
return [{ valueGroup: valueGroup, values: [value] }];
} else {
// look if valueGroup is already there
const index = selectedValues.findIndex((find) => find.valueGroup === valueGroup);
if (index === -1) {
// if no add new valueGroup with first values
return [...selectedValues, { valueGroup: valueGroup, values: [value] }];
} else {
// if valueGroup is there, look if value is already in values
const findedValues = selectedValues[index];
if (findedValues.values.includes(value)) {
// yes, remove value from values
return [...selectedValues, { ...findedValues, values: findedValues.values.filter((filter) => filter !== value) }];
} else {
// no, add value to values
return [
...selectedValues.filter((object) => object.valueGroup === valueGroup),
{ ...findedValues, values: [...findedValues.valueGroup, value] },
];
}
}
}
}
I always click a button. The button passes the values (valueGroup and value)
through the OnClick handler. The array should update itself according to my explanation. Here is an example of how that array could change
//Imagine, after some time we are here. At first it was empty. Two groups were inserted and filled
type SelectedValues = [
{
valueGroup: "valueGroupOne",
values: ["some", "more", "example", ]
},
{
valueGroup: "valueGroupTwo",
values: ["ddd", "sss", "aaa", ]
}
]
// And new group
type SelectedValues = [
{
valueGroup: "valueGroupOne",
values: ["some", "more", "example", ]
},
{
valueGroup: "valueGroupTwo",
values: ["ddd", "sss", "aaa", ]
},
valueGroup: "valueGroupThree",
values: ["ssss", "dddsd", "adsdas", ]
}
]
// add to an existing Group new value
type SelectedValues = [
{
valueGroup: "valueGroupOne",
values: ["some", "more", "example","newValueadded" ]
},
{
valueGroup: "valueGroupTwo",
values: ["ddd", "sss", "aaa", ]
},
valueGroup: "valueGroupThree",
values: ["ssss", "dddsd", "adsdas", ]
}
]
// delete from existing Group value
type SelectedValues = [
{
valueGroup: "valueGroupOne",
values: ["some", "more", "example" ]
},
{
valueGroup: "valueGroupTwo",
values: ["ddd", "sss", "aaa", ]
},
valueGroup: "valueGroupThree",
values: ["ssss", "dddsd", "adsdas", ]
}
]