📜  Lodash _.flatMapDeep() 方法

📅  最后修改于: 2022-05-13 01:56:36.221000             🧑  作者: Mango

Lodash _.flatMapDeep() 方法

Lodash 是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、集合、字符串、对象、数字等。

_.flatMapDeep()方法通过 iteratee函数运行给定集合中的每个元素来创建一个扁平的值数组,并递归地扁平化映射的结果。它类似于 _.flatMap() 方法。

句法:

_.flatMapDeep( collection, iteratee )

参数:此方法接受上面提到的两个参数,如下所述:

  • 集合:它是要迭代的集合。
  • iteratee:它是每次迭代调用的函数。

返回值:此方法返回新的展平数组。

示例 1:

// Requiring the lodash library 
const _ = require("lodash");
      
// Original array 
var users = (['aaa', 'bbb', 'ccc', 
              'ddd', 'eee', 'fff']);
  
// Using the _.flatMapDeep() method
let flat_map =
  _.flatMapDeep(users,
    function duplicate(n) {
      return [[[n, n]]];
  }
)
  
// Printing the output 
console.log(flat_map);

输出:

[
  'aaa', 'aaa', 'bbb', 'bbb',
  'ccc', 'ccc', 'ddd', 'ddd',
  'eee', 'eee'
]

示例 2:

// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
var user1 = ([1, 2, 3, 4, 5, 6, 7]);
var user2 = (['a', 'b', 'c', 'd', 'e']);
  
// Using the _.flatMapDeep() method
let flat_map1 =
  _.flatMapDeep(user1,
    function makePattern(n) {
      return [[[n, n + "->"]]];
  }
)
   
let flat_map2 =
  _.flatMapDeep(user2,
    function makePattern(n) {
      return [[["<-" + n, n]]];
  }
)
  
// Printing the output 
console.log(flat_map1);
console.log(flat_map2);

输出:

[
  1, 1->, 2, 2->, 3, 3->,
  4, 4->, 5, 5->, 6, 6->,
  7, 7->
]
[
  '<-a', 'a', <-'b', 'b',
  '<-c', 'c', '<-d', 'd',
  '<-e', 'e'
]