addOrRemove
Toggles a string or number inside an array. If the value already exists, the method returns a new array without it. If it does not exist, the method returns a new array with the value appended. This is useful for selected IDs, active filters, and checkbox-like state.
The method is declared with a this type of Array<string | number>, so TypeScript only allows it on string or number arrays. Membership is decided with includes(), which uses strict equality and therefore does not match across types.
Usage
ts
const selected = ['users', 'settings'];
const nextSelected = selected.addOrRemove('users');
// ['settings']Parameters
value: string | number
Value to remove when present or append when absent.
Returns
A new array with the value toggled.Uses
Type signature
ts
interface Array<T> {
addOrRemove(value: string | number): (string | number)[];
}