JavaScript对象合并,在覆盖对象时保持属性错误

时间:2017-03-07 10:37:55

标签: javascript object ecmascript-6

我遵循使用Object.assign函数

合并两个对象的情况
Object.assign(
    {}, 
    {
        a:1, 
        b: {
            c:{
                d: 1, 
                e: 1
            }
        }
    }, 
    {
        a:2, 
        b: {
            c: {
                d:2
            }
        }
    }
);

并获得

{
    a: 2,
    b: {
        c: {
            d: 2
        }
    }
}

但我希望得到

{
    a: 2,
    b: {
        c: {
            d: 2,
            e: 1
        }
    }
}

即,e不应该被淘汰。我想保留source1层次结构中source2层次结构中不存在的Object.assign层次结构中的属性。我是否以错误的方式使用$scope.myArray = [{ "pm_id": 2, "type": "Scrum", "estimated_end_date": "Wednesday, March 15, 2017", "no_of_sprints": 2, "status": "Not Started", "created_by": "admin@admin.com", "sprint": [{ "s_id": 4, "p_id": 35, "sprint_stage": "Sprint 1", "sprint_requirement": "description goes here", "sprint_start_date": "Monday, March 13, 2017", "sprint_end_date": "Friday, March 17, 2017" }, { "s_id": 5, "p_id": 35, "sprint_stage": "Sprint 2", "sprint_requirement": "description goes here", "sprint_start_date": "Monday, March 20, 2017", "sprint_end_date": "Friday, March 24, 2017" }] }];

0 个答案:

没有答案