Quite simple, really! Please keep in mind that ES7 might not be compatible with IE.
const myArray = [];
let addToArray = (newItem) => {
if ( myArray.includes(newItem) ) {
myArray.splice(myArray.findIndex(index => index == newItem), 1); // if element is in an array, find index & remove element
// number 1 in the end is to tell JS to remove exactly 1 element
// without it, we would wipe array elements all the way to the end
} else {
myArray.push(newItem); // else push it to array
}
}
Special thanks to Stack Overflow