I have a JavaScript array containing numbers, defined as follows:
var customerIds = [];
In my code, I have a function designed to manage the addition and removal of IDs from this array. The basic structure of my function is as follows:
function addOrRemove(shouldAdd, customerId) {
if (shouldAdd) {
if (customerIds.contains(customerId) === false) {
customerIds.push(customerId);
}
} else {
customerIds.remove(customerId);
}
}
This code represents a pseudo-function as JavaScript arrays do not have built-in 'contains' or 'remove' methods. My question is: Is there an elegant solution to this problem? Currently, I am simply manually looping through the array to find and track the index of the first occurrence.
I appreciate any insights or suggestions you may have on this matter.