Created
August 7, 2019 09:35
-
-
Save brunobuddy/36de2b973fd506024ce62b8d596060dc to your computer and use it in GitHub Desktop.
Simple Toggle Item from Array (add or remove) in TS / JS ES6
This file contains 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 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