Skip to content

Instantly share code, notes, and snippets.

@brunobuddy
Created August 7, 2019 09:35
Show Gist options
  • Save brunobuddy/36de2b973fd506024ce62b8d596060dc to your computer and use it in GitHub Desktop.
Save brunobuddy/36de2b973fd506024ce62b8d596060dc to your computer and use it in GitHub Desktop.
Simple Toggle Item from Array (add or remove) in TS / JS ES6
const selectedItemIds: number[] = []
toggleSelect(itemId: number) {
if (this.selectedItemIds.includes(itemId)) {
// Remove item from Array if already selected
const index = this.selectedItemIds.indexOf(itemId)
this.selectedItemIds.splice(index, 1)
} else {
// Adds item to Array
this.selectedItemIds.push(itemId)
}
console.log(this.selectedItemIds)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment