我想最近學習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)'更簡潔。 –
感謝您的建議。 –