Skip to content

Instantly share code, notes, and snippets.

@raidenz
Created October 25, 2025 09:55
Show Gist options
  • Select an option

  • Save raidenz/ed457f0da50474fed71d845d2cec9795 to your computer and use it in GitHub Desktop.

Select an option

Save raidenz/ed457f0da50474fed71d845d2cec9795 to your computer and use it in GitHub Desktop.
re loop dummy image
// use for fake data
const images = [1, 2, 3, 4]
const articles = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const paired = articles.map((article, i) => ({
article,
image: images[i % images.length],
}))
// ---
export function cycleArray<T>(source: T[], count: number): T[] {
return Array.from({ length: count }, (_, i) => source[i % source.length])
}
const cycledImages = cycleArray(images, articles.length)
// ---
export function cycleArray<T>(arr: T[], index: number): T {
return arr[index % arr.length]
}
const paired = articles.map((article, i) => ({
article,
image: cycleArray(images, i),
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment