Skip to content

Instantly share code, notes, and snippets.

@RayanAbid
Created November 21, 2022 16:00
Show Gist options
  • Save RayanAbid/0da93f177d514f9abc47cd85685cfd09 to your computer and use it in GitHub Desktop.
Save RayanAbid/0da93f177d514f9abc47cd85685cfd09 to your computer and use it in GitHub Desktop.
A js function that removes the value if exists and adds it if new
const arr = YOUR_ARRAY
const newId = YOUR_NEW_ID; //new id
if (!arr?.includes(newId)) {
console.log("add now");
arr?.push(newId); //adding to array because value doesnt exists
} else {
console.log("rem now");
arr?.splice(arr.indexOf(newId), 1); //deleting
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment