Ramda:如何使用 R.map 和两个参数?

Ramda: How to use R.map with two arguments?

一对数组效果很好。我不需要在这个例子中使用 Ramda。

const addChild1 =  (x , y) => `${x}.addChild(${y}); ` 
const result1 = addChild(["containerA"], ["a1","a2", "a3"])

console.log(result1) //containerA.addChild(a1,a2,a3) 

我无法使用二维字符串矩阵。我使用 R.map,但我在第二个参数中得到 'undefined'。

const addChild2 =  R.map ((x , y) => `${x}.addChild(${y}); `) 
const result2 = addChild2(["containerA", "containerB", "containerC"], [["a1","a2", "a3"], ["b1","b2", "b3"], ["c1","c2", "c3"]])

console.log(result2) //["containerA.addChild(undefined); ","containerB.addChild(undefined); ","containerC.addChild(undefined); "]

如何避免 'undefined'?理想的输出如下:

["containerA.addChild("a1","a2", "a3");","containerB.addChild("b1","b2", "b3 ");","containerC.addChild("c1","c2", "c3");"]

map 将字符串作为第二个参数,因此在这种情况下,只有 ["containerA", "containerB", "containerC"] 进入循环

在这种情况下您应该使用 zipWith 而不是 map

const addChild2 = R.zipWith((x, y) => `${x}.addChild(${y}); `)
const result2 = addChild2(
  ["containerA", "containerB", "containerC"],
  [
    ["a1", "a2", "a3"],
    ["b1", "b2", "b3"],
    ["c1", "c2", "c3"],
  ]
)

console.log(result2)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>