UNPKG

mathjs

Version:

Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser and offers an integrated solution to work with numbers, big numbers, complex numbers, units, and matrices.

41 lines (37 loc) 1.26 kB
module.exports = function (math) { var isMatrix = require('../../type/Matrix').isMatrix; /** * Execute a callback method on each entry of the matrix or the array. * @param {Matrix/array} x The container to iterate on. * @param {function} callback The callback method is invoked with three * parameters: the value of the element, the index * of the element, and the Matrix/array being traversed. */ math.forEach = function (x, callback) { if (arguments.length != 2) { throw new math.error.ArgumentsError('forEach', arguments.length, 2); } if (Array.isArray(x)) { return _forEachArray(x, callback); } else if (isMatrix(x)) { return x.forEach(callback); } else { throw new math.error.UnsupportedTypeError('forEach', math['typeof'](x)); } }; function _forEachArray (array, callback) { var index = []; var recurse = function (value, dim) { if (Array.isArray(value)) { value.forEach(function (child, i) { index[dim] = i; // zero-based index recurse(child, dim + 1); }); } else { callback(value, index, array); } }; recurse(array, 0); }; };