Created
November 21, 2022 16:00
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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