将索引项附加到对象的正确 ES6 方法是什么?

What is the proper ES6 way for appending an indexed item to an Object?

如果我有这样的对象:

var state = { 10: [Object], 12: [Object, Object], 13: [Object, Object, Object] }

我想在不改变原始对象的情况下将一个新对象添加到其中一个子数组的末尾。相反,通过 ES6 返回一个新对象:

不可否认,我对不可变对象和 ES6 的理解都很愚蠢。这是我的第一次尝试,但这(出于显而易见的原因)不起作用。

  return  {
    ...state,
    [12]:
      state[12], Object
  }

有人知道产生这个的正确语法吗? :

var state = { 10: [Object], 12: [Object, Object, Object], 13: [Object, Object, Object] }
                                                 ^---- One extra object at the end of `12`

听起来您正在寻找

return Object.assign({}, state, {12: state[12].concat([newObject])});

或使用 ES6 数组扩展语法

return Object.assign({}, state, {12: [...state[12], newObject]});

或者用实验对象传播语法

return {...state, 12: [...state[12], newObject]};