首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >递归flatMap

递归flatMap
EN

Stack Overflow用户
提问于 2021-11-03 11:08:50
回答 4查看 78关注 0票数 2

我正在尝试弄清楚如何将下面的javascript函数转换为递归执行flapMap的动态函数。

代码语言:javascript
复制
function getPermutations(object) {

  let array1 = object[0].options,
    array2 = object[1].options,
    array3 = object[2].options;

  return array1.flatMap(function(array1_item) {
    return array2.flatMap(function(array2_item) {
      return array3.flatMap(function(array3_item) {
        return array1_item + ' ' + array2_item + ' ' + array3_item;
      });
    });
  });
}

let object = [{
  "options": ['blue', 'gray', 'green']
}, {
  "options": ['large', 'medium', 'small']
}, {
  "options": ['wood', 'steel', 'pastic']
}];


console.log('Permutations', getPermutations(object));

在本例中,我向函数发送了3个数组,这就是为什么它有3次flapMap迭代的原因。运行良好,但我试图使它成为动态的,这样我就可以传递一个动态数组,该函数将根据该数组递归地执行flapMap。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2021-11-03 11:33:17

一种方法是使用reduce

您希望将选项列表减少为一个排列列表。

代码语言:javascript
复制
function getPermutations(list) {
  return (
    list
      // First map to list of options (list of list of strings)
      .map((item) => item.options)
      // Then reduce. Do not set any initial value.
      // Then the initial value will be the first list of options (in
      // our example, ["blue", "gray", "green"])
      .reduce((permutations, options) => {
        return permutations.flatMap((permutation) =>
          options.map((option) => permutation + " " + option)
        );
      })
  );
}

// Renamed this to list, since it is an array and not an object
const list = [
  { options: ["blue", "gray", "green"] },
  { options: ["large", "medium", "small"] },
  { options: ["wood", "steel", "pastic"] },
];

console.log("Permutations", getPermutations(list));

编辑:我知道你要求递归。如果这是一项学校任务,那么也许你必须使用递归,否则我建议尽可能避免递归,因为它往往会使事情变得更加复杂。(当然,这是一个通用规则,与所有规则一样,它也有一些例外。)

票数 1
EN

Stack Overflow用户

发布于 2021-11-03 11:22:58

在您的示例中,您跟踪了array1_itemarray2_item和co各自的变量。您可以将它们移动到一个数组中(具有动态大小;我称之为_prevItems),并将它们作为参数传递给递归调用。

代码语言:javascript
复制
function getPermutations(objects, _prevItems = []) {
  // join the items at the end of the recursion
  if (objects.length === 0)
    return _prevItems.join(' ')
  
  // call again with all but the first element, and add the current item to _prevItems
  return objects[0].flatMap(item => getPermutations(objects.slice(1), [..._prevItems, item]))
}

let objects = [['blue', 'gray', 'green'], ['large', 'medium', 'small'], ['wood', 'steel', 'pastic']];


console.log('Permutations', getPermutations(objects));

票数 1
EN

Stack Overflow用户

发布于 2021-11-03 11:46:59

可以使用递归的自下而上方法一次flatMap两个数组,并从最后构建字符串

代码语言:javascript
复制
const getPermutations = (array) => {
  if(array.length === 1)
     return array[0].options;

  const prefixItems = array[0].options;
  const suffixItems = getPermutations(array.slice(1));

  return prefixItems.flatMap(prefix => {
    return suffixItems.flatMap(suffix => {
      return prefix + ' ' + suffix
    });
  })
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69823796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档