Skip to content

Instantly share code, notes, and snippets.

@AngeloAnolin
Created January 19, 2024 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AngeloAnolin/5712d6bae230badfe243558a08a16d77 to your computer and use it in GitHub Desktop.
Save AngeloAnolin/5712d6bae230badfe243558a08a16d77 to your computer and use it in GitHub Desktop.
Flip 2D array vertically or horizontally.
let array = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
const flip = (array, direction = 'HORIZONTAL') => {
const temp = [...array]
if (direction.toUpperCase() === 'VERTICAL') {
return (temp.reverse())
} else {
return (temp.map(arr => arr.reverse()))
}
}
console.log('HOR', flip(array, 'HORIZONTAL')) // "HOR", [[3, 2, 1], [6, 5, 4], [9, 8, 7]]
console.log('VERT', flip(array, 'VERTICAL')) // "VERT", [[9, 8, 7], [6, 5, 4], [3, 2, 1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment