Dam*_*e31 9 javascript merge object lodash
我已经研究了如何在省略空值的同时合并两个JavaScript对象,到目前为止我已经尝试过使用merge,assign,clone而没有成功.
这是我的测试(JSFiddle):
let defaultValues = {code: '', price: 0, description: ''}
let product = {code: 'MyCode', price: null, description: 'Product Description'}
//Merge two objects
let merged = _.merge({}, defaultValues, product)
console.log(merged)
//My result
{code: 'MyCode', price: null, description: 'Product Description'}
//My expected result
{code: 'MyCode', price: 0, description: 'Product Description'}
Run Code Online (Sandbox Code Playgroud)
我使用VueJS框架,当我在某些输入(使用v-model)上有这些null属性时,我收到一个异常.
谢谢!
Ry-*_*Ry- 15
用途_.mergeWith:
let merged = _.mergeWith(
{}, defaultValues, product,
(a, b) => b === null ? a : undefined
)
Run Code Online (Sandbox Code Playgroud)