array-each-nonflat
Array each to iterate on both flat and non-flat arrays via a non-recursive single-loop.
Install
$ npm i array-each-nonflat
Usage
Basically, your function will receive each element (first argument) in order as it goes thru the array from beginning to end, including into any sub-arrays it encounters from beginning to end.
The usual index argument (second argument) is the same as you would expect of a flat array. It's where the element would be if the array was flattened. That makes it different than the usual value because it's not the index into the current array (third argument).
The usual array argument (third argument) is the array the element is from, so, it may be an inner array instead of the main outermost array provided to each
.
A new fourth argument is the index into the current array for the current element.
A new fifth argument is the stack of outer arrays and their next index to process, once we complete the current inner array. Each stack element is an array with two elements. The first is the array and the second is the index in that array to do next, when we get back to that array.
const each = require('array-each-nonflat')
const innerArray1 = [2, 3]
const innerArray3 = [5, 6]
const innerArray2 = [4, innerArray3, 7, 8]
const array = [ 1, innerArray1, innerArray2, 9 ]
each(array, (e, I, a, i, s) => {
})