mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
26 lines
840 B
JavaScript
26 lines
840 B
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Execute the callback function element wise for each element in array and any
|
|
* nested array
|
|
* Returns an array with the results
|
|
* @param {Array | Matrix} array
|
|
* @param {Function} callback The callback is called with two parameters:
|
|
* value1 and value2, which contain the current
|
|
* element of both arrays.
|
|
* @param {boolean} [skipZeros] Invoke callback function for non-zero values only.
|
|
*
|
|
* @return {Array | Matrix} res
|
|
*/
|
|
module.exports = function deepMap(array, callback, skipZeros) {
|
|
if (array && (typeof array.map === 'function')) {
|
|
// TODO: replace array.map with a for loop to improve performance
|
|
return array.map(function (x) {
|
|
return deepMap(x, callback, skipZeros);
|
|
});
|
|
}
|
|
else {
|
|
return callback(array);
|
|
}
|
|
};
|