Object.assign—override nested property

前端 未结 7 453
轮回少年
轮回少年 2020-12-13 01:36

I have an Object a like that:

const a = {
  user: {
   …
   groups: […]
   …
  }
}

whereby there are a lot more properties in

7条回答
  •  执念已碎
    2020-12-13 02:23

    You asked specifically for an ES6 way with Object.assign, but maybe someone else will prefer my more 'general' answer - you can do it easily with lodash, and personally I think that this solution is more readable.

    import * as _ from 'lodash';
    _.set(a, 'user.groups', newGroupsValue);
    

    It mutates object.

提交回复
热议问题