当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《如何使用另一个的值覆盖一个Object(lodash)》经验,为你挑选了1个好方法。

我正在寻找一个函数,如果它存在,覆盖一个对象的值与另一个对象的值,但我没有在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> Max Sindwani..:

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' }
推荐阅读
php
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有