Collection of convenience code snippets (helpers) that aims to make it a little easier to work with vanilla JS
vanillajs-helpers > “indexLoop”
Τ IndexLoopCallback: function
Defined in indexLoop.ts:5
►(item: any
, index: number
, arr: ArrayLike
.<any
>): void
⎮boolean
Parameters:
Param | Type | Description |
---|---|---|
item | any |
- |
index | number |
- |
arr | ArrayLike .<any > |
- |
Returns: void
⎮boolean
► indexLoop(collection: any
[]⎮ArrayLike
.<any
>, cb: IndexLoopCallback): number
Defined in indexLoop.ts:25
Iterate over numeric indexes in an object (use return false
to break the loop prematurely).
const len = indexLoop([1,2,3], (n) => { console.log(n); }); // -> 1 ... 2 ... 3
// len === 3
// With premature break
indexLoop([1,2,3], (n) => { console.log(n); return n < 3 }); // -> 1 ... 2
Parameters:
Param | Type | Description |
---|---|---|
collection | any []⎮ArrayLike .<any > |
The collection to iterate over |
cb | IndexLoopCallback | The function to call on each iteration |
Returns: number