我想最近学习fp,并尝试使用ramda.js到我的项目,但我遇到了一些问题。与ramda.js代码有什么不同?
我的代码的目标是我想将result
prop添加一些初始值到list
数组的每个元素中,但它没有按预期工作。
这里是我的代码:
var list = [{name: 'foo'}, {name: 'bar'}, {name: 'baz'}]
var list1 = R.clone(list)
var getResultList = R.times(R.identity)
// what's the difference between them ??
// it worked as expected if I wrap assoc fn into arrow function and transfer it to fn
var mapAssocResult= R.map(e=> R.assoc('result', getResultList(2), e))
// it didn't work as expected if I just transfer it as param of map fn
var mapAssocResult1= R.map(R.assoc('result', getResultList(2)))
list = mapAssocResult(list)
list1 = mapAssocResult1(list1)
list[0].result === list[1].result //false
list1[0].result === list1[1].result // true
// it seems that all result props point to the same array reference, but why?
是不是我需要使用Ramda.js注意?
也许我在使用Ramda.js的想法是完全错误的,那么是否有更合理的方法来实现我在Ramda.js中的目标?
非常感谢。
我并没有超出斯科特克里斯托弗下面的建议,但我会注意到'R.range(0)'可能更具描述性,并且肯定比'R.times(R.identity)'更简洁。 –
感谢您的建议。 –