如何在休息操作中进行字符串插值

How to do string interpolation in rest operation

谁能告诉我使用解构从对象中删除 属性 的最佳方法?我想从下面的对象中删除单个任务

Object {task111: Object, task113: Object, task114: Object}

const taskId = `task${action.task.id}`;
{ [taskId]: discard, ...newState } = state;
return newState;

错误:

 ./src/reducers/taskReducer.js
Syntax error: Unexpected token, expected ; (32:13)

  30 |      
  31 |          const taskId = `task${action.task.id}`
> 32 |          { [taskId]: discard, ...newState } = state;
     |                    ^
  33 |          return newState
  34 |   

您使用的是 computed property name,而不是字符串插值,您需要将 属性 值分配给一个变量才能正常工作。

const state = {task111: { a: 1 }, task113: { b: 2 }, task114: { c:  3 }};

const action = {
  task: {
    id: 113
  }
};

const taskId = `task${action.task.id}`;

const { [taskId]: discard, ...newState } = state;

console.log(newState);