用来自不同集合的元素替换数组中的项目

Replace items in array with elements from a different set

我不确定以递归方式处理此问题的正确方法。 假设我有两个数组

array1 = [a, null, c, d, e]

array2 = [1, 2, 3]

我想创建一个形式为

的结果数组
[
   [a, 1, c, d, e],
   [a, 2, c, d, e],
   [a, 3, c, d, e]
]

其中第二个数组的每个元素填充空的地方。我能够很好地做到这一点,但是如果我有两个 null 的情况怎么办?

array1 = [a, null, c, null, e]

array2 = [1, 2, 3]

如何获得以下内容?

[
   [a, 1, c, 1, e],
   [a, 1, c, 2, e],
   [a, 1, c, 3, e],
   [a, 2, c, 1, e],
   ...
   [a, 3, c, 3, e]
]

您描述的第一个案例是 map 模式的实例,第二个案例 -- flatMap.

换句话说,map 就像一个循环,而 flatMap 就像一个 two-level 嵌套循环,从最深层次的循环中一个一个地创建结果的元素:

地图:

      for x in [1, 2, 3]:
         let res = { result of replacing
                     the first `null` with `x` 
                     in [a, null, c, d, e] }
         yield res

             -------------------------
               [a, null, c, d, e]
             ----------------------
   1           [a,  1,   c, d, e],
   2           [a,  2,   c, d, e],
   3           [a,  3,   c, d, e]

平面地图:

      for x in [1, 2, 3]:
         let res = { result of replacing
                     the first `null` with `x` 
                     in [a, null, c, null, e] }
         
         for x in [1, 2, 3]:
             let res2 = { result of replacing
                          the first `null` with `x` 
                          in res }                   ## res NB!
             yield res2

             -------------------------
             [  a, null, c, null, e  ]
             -------------------------
   1:        [  a,  1,   c, null, e  ],

        1      [a,  1,   c,   1 , e],
        2      [a,  1,   c,   2 , e],
        3      [a,  1,   c,   3 , e],

   2:        [  a,  2,   c, null, e  ],

        1      [a,  2,   c,   1 , e],
        2      [a,  2,   c,   2 , e],
        3      [a,  2,   c,   3 , e],

   3:        [  a,  3,   c, null, e  ]

        1      [a,  3,   c,   1 , e],
        2      [a,  3,   c,   2 , e],
        3      [a,  3,   c,   3 , e]

第一个就像用替换结果替换[1,2,3]中的每个元素;第二个就像用替换结果的 list 替换 [1,2,3] 中的每个元素,并将结果列表 append 在一起;即拼接结果列表的元素到位;即创建组合地图,flattened(因此得名,flatMap)。

Javascript 上的代码:

let array1 = ['a', null, 'c', null, 'e']
let array2 = [1, 2, 3]

let output = []
let cursorPosition = 0; // cursorPosition is a pointer for `array2` index
let nullPositions = []

// find nullable positions; in our case here will be 1, 4
for (let i = 0; i < array1.length; i++) {
  if (array1[i] === null) {
    nullPositions.push(i)
  }
}

// find total expected numbers of arrays from given input and create that array
const totalArray = Math.pow(array2.length, nullPositions.length);
for (let i = 0; i < totalArray; i++) {
  output.push([...array1])
}

// replacement logic for the `null` values
for (let i = 0; i < nullPositions.length; i++) {
  cursorPosition = 0;
  // frequency: how often the cursor needs to change (depends on null position)
  let frequency = totalArray / Math.pow(array2.length, i + 1);
  let resetCursorPositionOn = frequency;
  for (let j = 0; j < output.length; j++) {
    if (j < resetCursorPositionOn) {
      output[j][nullPositions[i]] = array2[cursorPosition];
    } else {
      resetCursorPositionOn += frequency;
      cursorPosition++;
      if (cursorPosition === array2.length) {
        cursorPosition = 0;
      }
      output[j][nullPositions[i]] = array2[cursorPosition];
    }
  }
}
console.log(output);

输出:

[
  ["a",1,"c",1,"e"],
  ["a",1,"c",2,"e"],
  ["a",1,"c",3,"e"],
  ["a",2,"c",1,"e"],
  ["a",2,"c",2,"e"],
  ["a",2,"c",3,"e"],
  ["a",3,"c",1,"e"],
  ["a",3,"c",2,"e"],
  ["a",3,"c",3,"e"]
]

它可以任意组合。