1.0.9 โข Published 1 year ago
@sensethenlove/loop-backwards v1.0.9
๐ @sensethenlove/loop-backwards
๐ Description
Loop an array backwards! Give loopBackwards() an array and a callback. On each iteration calback provides the current item, a splice function, and the current index. If the splice function is called the item is removed from the array. If the splice function is called & the current item is a DOM element the item is removed from the array & the item is removed from the DOM
โฏ๏ธ Install
pnpm add @sensethenlove/loop-backwards
๐ Example: Array of numbers
import loopBackwards from '@sensethenlove/loop-backwards'
loopBackwards(numbers, (number, splice, index) => {
if (number === 9 || index === 9) splice()
})
๐งก Example: Array of objects
loopBackwards(items, ({ foo }, splice) => {
if (foo === 'bar') splice()
})
๐ Example: Array of DOM elements
const elements = document.querySelectorAll('div')
loopBackwards(Array.from(elements), (element, splice) => {
if (element.getAttribute('foo') === 'bar') splice()
})