Skip to content

invokeMap

js
_.invokeMap(collection, path, [args])

调用 collection 中每个元素的 path 处的方法,返回每个调用方法的结果数组。将向每个调用的方法提供任何其他参数。如果 path 是一个函数,它会被调用,并且 this 会绑定到 collection 中的每个元素。

¥Invokes the method at path of each element in collection, returning an array of the results of each invoked method. Any additional arguments are provided to each invoked method. If path is a function, it's invoked for, and this bound to, each element in collection.

新增于

¥Since

4.0.0

参数

¥Arguments

  1. collection (数组|对象):要迭代的集合。

    ¥collection (Array|Object): The collection to iterate over.

  2. path (数组|函数|字符串):要调用的方法的路径或每次迭代调用的函数。

    ¥path (Array|Function|string): The path of the method to invoke or the function invoked per iteration.

  3. [args] (...*):用于调用每个方法的参数。

    ¥[args] (...)*: The arguments to invoke each method with.

返回

¥Returns

(数组):返回结果的数组。

¥(Array): Returns the array of results.

示例

¥Example

js
_.invokeMap([[5, 1, 7], [3, 2, 1]], 'sort');
// => [[1, 5, 7], [1, 2, 3]]

_.invokeMap([123, 456], String.prototype.split, '');
// => [['1', '2', '3'], ['4', '5', '6']]

Lodash v4.17 中文网 - 粤ICP备13048890号