小编典典

用uuid键修改对象数组

reactjs

我要加

我已经删除

现在我需要修改

只是随意添加

delete能够以外科手术的精确度完成工作,因为它使用键来找到罪魁祸首:

addInput = (name) => {
    const newInputs = this.props.parameters;
    newInputs.push({
        name,
        key: uuid(),
        value: { input: '' },
        icon: { inputIcon: 0 },
    });
    this.setState({
        newInput: newInputs,
    });
    this.props.exportParameter(newInputs);
};

removeInput = (key) => {
    const newInputs = this.props.parameters.filter(x => x.key !== key);
    this.setState({
        newInput: newInputs,
    });
    this.props.exportParameter(newInputs);
};

如何修改(例如,将值设置回“”而不删除并重新创建项目)?

modifyInput = (key) => {
    ?????
};

阅读 307

收藏
2020-07-22

共1个答案

小编典典

您可以使用 Array.prototype.find()

modifyInput = (key) => {
  const match = this.props.parameters.find(x => x.key === key);
  // do stuff with matched object
};
2020-07-22