UNPKG

682 BJavaScriptView Raw
1'use strict'
2
3const isMatrix = require('./isMatrix')
4
5/**
6 * Recursively loop over all elements in a given multi dimensional array
7 * and invoke the callback on each of the elements.
8 * @param {Array | Matrix} array
9 * @param {Function} callback The callback method is invoked with one
10 * parameter: the current element in the array
11 */
12module.exports = function deepForEach (array, callback) {
13 if (isMatrix(array)) {
14 array = array.valueOf()
15 }
16
17 for (let i = 0, ii = array.length; i < ii; i++) {
18 const value = array[i]
19
20 if (Array.isArray(value)) {
21 deepForEach(value, callback)
22 } else {
23 callback(value)
24 }
25 }
26}