Javascript 使用uuid键修改对象数组

Javascript 使用uuid键修改对象数组,javascript,arrays,reactjs,react-native,Javascript,Arrays,Reactjs,React Native,我有补充 我要删除 现在我需要修改 “添加”只是随意添加到堆中 delete能够以外科手术般的精度完成其工作,因为它使用密钥查找罪魁祸首: addInput = (name) => { const newInputs = this.props.parameters; newInputs.push({ name, key: uuid(), value: { input: '' }, icon: { inputIc

我有补充

我要删除

现在我需要修改

“添加”只是随意添加到堆中

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) => {
    ?????
};
您可以使用Array.prototype.find

您可以使用Array.prototype.find

您可以映射参数,然后在找到匹配项时进行修改:

modifyInput = (key) => {   
    const newInputs = this.props.parameters.map(x => {
        if (x.key === key) {
           x.modification = true;
        }
        return x;
    });
    this.setState({
        newInput: newInputs,
    });
    this.props.exportParameter(newInputs);
};
您可以映射参数,然后在找到匹配项时进行修改:

modifyInput = (key) => {   
    const newInputs = this.props.parameters.map(x => {
        if (x.key === key) {
           x.modification = true;
        }
        return x;
    });
    this.setState({
        newInput: newInputs,
    });
    this.props.exportParameter(newInputs);
};

不删除并重新创建项目是。如果不删除并重新创建itemYes,则这两者都不起作用。这两者都没有
var empMap= {};
    //initialize empMap with key as Employee ID and value as Employee attributes:
    //Now when salary updated on UI from 100 - 300 you can update other fields 
    var e=empMap[id];
    if(e){
        e.Salary=newSalary;
        e.YearlySalary = newSalary*12;
        e.Deductions = e.YearlySalary*0.2;
        empMap[id]=e;

    }