我正在寻找一个函数,如果它存在,覆盖一个对象的值与另一个对象的值,但我没有在lodash
文档中找到它
var primary = { name: 'egg' weight: '2oz', meal: 'breakfast' } var secondary = { name: 'egg', weight: '3oz' state: 'uncracked' } var result = _.override(primary, secondary); console.log(result) // => { name: 'egg' weight: '3oz', meal: 'breakfast, state: 'uncracked' }
如何在函数式编程中实现这一目标?
Checkout Lodash的_.assign
功能 - https://lodash.com/docs/4.17.2#assign.
Note: This method mutates object and is loosely based on Object.assign.
var primary = {
name: 'egg',
weight: '2oz',
meal: 'breakfast'
};
var secondary = {
name: 'egg',
weight: '3oz',
state: 'uncracked'
};
var result = _.assign({}, primary, secondary);
console.log(result)
// => { name: 'egg' weight: '3oz', meal: 'breakfast, state: 'uncracked' }