使用不变性助手`update`时如何使用我的索引作为键

How to use my index as the key when using immutability helper`update`

我正在尝试使用 React 文档中建议的 immutability-helper 更新我的列表,您可以看到我当前用于更新的代码

    const oldInstallment = this.findInstallmentByIndex(this.state.installmentList, index);
    let newInstallment =  {...oldInstallment}
    newInstallment.isActivated = isActivated;

    const newInstallmentList = update(this.state.installmentList, {index: {$set: newInstallment}});
    this.setState({installmentList: newInstallmentList});

我目前遇到的问题是 index 没有用作值,而是用作名为 index 的键,意思不是说 0:{$set: newInstallment} 代码的作用是 index:{$set: newInstallment} 这导致向数组添加新元素而不是更新 index 0
中的元素 所以我的问题是我如何告诉这个更新方法使用我的索引值作为键?!

试试这样写:

const newInstallmentList = update(this.state.installmentList, {[index]: {$set: newInstallment}});