Lodash - flatMapDepth 方法

语法

_.flatMapDepth(collection, [iteratee=_.identity], [depth=1])

此方法与 _.flatMap 类似,不同之处在于它以递归方式将映射结果展平至深度倍数。

参数

  • collection (Array|Object) − 要迭代的集合。

  • [iteratee=_.identity] (函数) − 每次迭代调用的函数。

  • [depth=1] (数字) −最大递归深度。

输出

  • (Array) − 返回新的扁平数组。

示例

var _ = require('lodash');
var list = [1 , 2, 3, 4, 5];

var result = _.flatMapDepth(list, function(n) {
    return [[[n, n]]];
},2);

console.log(result);

将上述程序保存在 tester.js 中。运行以下命令执行此程序。

命令

\>node tester.js

输出

[ [ 1, 1 ], [ 2, 2 ], [ 3, 3 ], [ 4, 4 ], [ 5, 5 ] ]

lodash_collection.html