函数 map #

创建一个新的矩阵或数组,其中包含对给定矩阵/数组的每个条目执行回调函数的结果。

对于输入的每个条目,

回调函数将被调用,并带有 2N + 1 个参数:条目的 N 个值、该条目发生的索引,以及正在遍历的 N 个完整的广播矩阵/数组,其中 N 是正在遍历的矩阵的数量。请注意,由于矩阵/数组可能是多维的,“索引”参数始终是一个数字数组,给出每个维度的索引。对于向量也是如此:“索引”参数是长度为 1 的数组,而不是简单的数字。

Syntax #

math.map(x, callback)
math.map(x, y, ..., callback)

Parameters #

Parameter Type Description
x 矩阵 | 数组 要迭代的输入。
回调函数 Function 要对输入的每个条目调用的函数(如上所述)

Returns #

Type Description
矩阵 | 数组 x 的变换后的 map;始终具有与 x 相同的类型和形状

Throws #

Type | Description —- | ———–

Examples #

math.map([1, 2, 3], function(value) {
  return value * value
})  // returns [1, 4, 9]
math.map([1, 2], [3, 4], function(a, b) {
 return a + b
})  // returns [4, 6]

// The callback is normally called with three arguments:
//    callback(value, index, Array)
// If you want to call with only one argument, use:
math.map([1, 2, 3], x => math.format(x)) // returns ['1', '2', '3']
// It can also be called with 2N + 1 arguments: for N arrays
//    callback(value1, value2, index, BroadcastedArray1, BroadcastedArray2)

另请参阅 #

filter, forEach, sort

Fork me on GitHub