Skip to content

Instantly share code, notes, and snippets.

@alekstar79
Created October 16, 2023 16:13
Show Gist options
  • Save alekstar79/c48a718f6043b16f53167c3c841c718f to your computer and use it in GitHub Desktop.
Save alekstar79/c48a718f6043b16f53167c3c841c718f to your computer and use it in GitHub Desktop.
function zip(...arrays) {
const maxLength = Math.max(...arrays.map(array => array.length))
return Array.from({ length: maxLength }).map((_, i) => {
return Array.from({ length: arrays.length }, (_, j) => arrays[j][i])
})
}
// Define the arrays that contain the coordinates
const xCoordinates = [1, 2, 3, 4]
const yCoordinates = [5, 6, 7, 8]
const zCoordinates = [3, 6, 1, 7]
// Create a zipped array of points
const points = zip(xCoordinates, yCoordinates, zCoordinates)
// Use the zipped array of points
console.log(points) // [[1, 5, 3], [2, 6, 6], [3, 7, 1], [4, 8, 7]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment