2017-10-12 61 views
1

我得补充修改对象以uuid数组中的键

我有删除

现在我需要修改

添加只是增加了一堆随意

删除是能够做到这一点是外科医生的精准度工作,因为它使用了键查找它的罪魁祸首:

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) => { 
    ????? 
}; 

回答

3

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

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

您可以map通过参数,然后修改,当你发现一个匹配:

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); 
}; 
+0

_“不删除并重新创建项目)“_ – guest271314

+0

是的。这既不 –

1
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; 

    }