Created
October 18, 2022 08:04
-
-
Save masiur/97264bdc24309bb27779d40fbeed0a86 to your computer and use it in GitHub Desktop.
Javascript move left/right of elements in array
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
let stage='completed'; | |
this.statuses = ['open', 'in-progress', 'completed']; | |
let index = this.statuses.indexOf(stage); | |
if (direction === 'left') { | |
if (index > 0) { | |
this.statuses.splice(index, 1); | |
this.statuses.splice(index - 1, 0, stage); | |
} | |
} else if (direction === 'right') { | |
if (index < this.statuses.length - 1) { | |
this.statuses.splice(index, 1); | |
this.statuses.splice(index + 1, 0, stage); | |
} | |
} | |
console.log(this.statuses); |
Author
masiur
commented
Oct 19, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment