用map和object.assign替换嵌套的对象

时间:2018-09-29 18:04:52

标签: javascript ecmascript-6

我有一个对象-

{
    "year2018": {
        "student": {
            "class8": [
                {
                    "email": "bob.manuel@gmail.com",
                    "fullName": "Bob Manuel"
                },
                {
                    "email": "Sam.manuel@gmail.com",
                    "fullName": "Sam Manuel"
                }
            ],
            "class7": [
                {
                    "email": "mona@gmail.com",
                    "fullName": "Mona kelly"
                }
            ]
        },
        "fee": {
            "code": "f1",
            "name": "International"
        },
        "campus": {
            "name": "ABC",
            "sqft": 1000
        }
    }
}

以上对象来自服务,其为- this.getSettings()。take(1)

现在,我有一个新对象,例如-

"class7": [
                {
                    "email": "mona@gmail.com",
                    "fullName": "Mona kelly"
                },
                {
                    "email": "nikki@gmail.com",
                    "fullName": "Nikki Nona"
                }
            ]

我想在原始对象中替换该对象,所以我的新对象看起来像

{
    "year2018": {
        "student": {
            "class8": [
                {
                    "email": "bob.manuel@gmail.com",
                    "fullName": "Bob Manuel"
                },
                {
                    "email": "Sam.manuel@gmail.com",
                    "fullName": "Sam Manuel"
                }
            ],
            "class7": [
                {
                    "email": "mona@gmail.com",
                    "fullName": "Mona kelly"
                },
                {
                    "email": "nikki@gmail.com",
                    "fullName": "Nikki Nona"
                }
            ]
        },
        "fee": {
            "code": "f1",
            "name": "International"
        },
        "campus": {
            "name": "ABC",
            "sqft": 1000
        }
    }
}

我当前的代码如下-

var KEY = year2018, key1= student, key2 = class7;
const saveStream = this.getSettings().take(1).map(settings => ({ [KEY]: Object.assign(settings, { [key1]: value }) })).flatMap(dt => this.http.put(this.url, dt).map(res => dt)).pluck(KEY).share();

。符号对我不起作用,我想使用键变量

0 个答案:

没有答案