我正在将对象合并在一起,但我遇到的一个问题是,当我将一个对象与列表合并时,它不会使用另一个对象,而是将它们组合在一起。有什么办法可以解决这个问题吗?
即。
obj1 = fromJS({name: 'kim', friends: [1,2,3]})
obj2 = fromJS({pet: 'Alex', friends: [4,5,6]})
obj1.mergeWith(obj2)
===> Desired Result
obj3 = fromJS({name: 'kim', pet: 'Alex', friends: [4,5,6]})
===> Actual Result
obj3 = fromJS({name: 'kim', pet: 'Alex', friends: [1,2,3,4,5,6]})
最佳答案
immutable 为您提供了一个 mergeWith 函数,可让您定义如何处理冲突。
export function mergeWithoutList(prev, next){
if(typeof next == 'object'){
if(List.isList(prev)){
return next
}else{
return prev.mergeWith(mergeWithoutList, next)
}
}
return next
}
obj3 = obj1.mergeWith(mergeWithoutList, obj2)
=>
obj3 == fromJS({name: 'kim', pet: 'Alex', friends: [4,5,6]})
关于immutable.js - immutablejs mergeWith 对象但覆盖新列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37192616/