如何使用另一个的值覆盖一个Object(lodash)

时间:2017-01-22 03:18:42

标签: javascript lodash

我正在寻找一个功能,如果它存在,则覆盖一个对象的值与另一个对象的值,但我没有在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' }

如何在函数式编程中实现这一目标?

1 个答案:

答案 0 :(得分:5)

结帐Lodash的Note: This method mutates object and is loosely based on Object.assign.功能 - https://lodash.com/docs/4.17.2#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' }



<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script></script>
&#13;
@for (int i = 1; i <= 10; i++)
{
    @i
}
&#13;
&#13;
&#13;