简体   繁体   English

React-如何将动态键/值对添加到对象?

[英]React - how to add dynamic key/value pair to an object?

I am trying to add dynamic key/value pair to an initial object with the appendInput() function. 我正在尝试使用appendInput()函数将动态键/值对添加到初始对象。

Initial object: 初始对象:

constructor(props) {
super(props);
this.state = {
  milestonesValues : {
    milestone0: "dssdsad",
    milestone1: "",
    milestone2: "",
    milestone3: "",
  }
};

} }

The render method: 渲染方法:

render(){
return(
  <div>
    <main className="content">
      <form onSubmit={this.onSubmit}>
        <div className="input-wrap">
          <label>{'What are the basic steps?'}
            {Object.keys(this.state.milestonesValues).map( (milestone, index) =>
              <input
                key={milestone}
                placeholder={`${index+1}.` }
                type="text"
                name={milestone}
                value={this.state.milestonesValues[milestone]}
                onChange={this.handleInputChange} />
              )}
          </label>
          <button onClick={ () => this.appendInput() }>
            {"+ ADD MILESTONE"}
          </button>
        </div>
       </form>
     </main>
   </div>
);

appendInput() function: appendInput()函数:

  appendInput() {
var objectSize = Object.keys(this.state.milestonesValues).length;
var newInput = `milestone${objectSize}: "",`;

console.log(newInput);
this.setState({
  milestonesValues: this.state.milestonesValues.concat([newInput])
});

} }

and I just can't add the new generated key/value to that initial object. 我只是无法将新生成的键/值添加到该初始对象。

Can someone help, please? 有人可以帮忙吗?

You can accomplish what you want using computed expression (I'm not really sure if you are trying to do that already). 您可以使用计算表达式来完成所需的操作(我不确定是否已经尝试这样做)。 So your appendInput function should look something like this: 因此,您的appendInput函数应如下所示:

 appendInput() { var objectSize = Object.keys(this.state.milestonesValues).length; var newInput = Object.assign({}, this.state.milestonesValues, {['milestone'+ objectSize]: ''}); this.setState({ milestonesValues: newInput) }); } 

Use this: 用这个:

appendInput() {
    var milestonesValues = Object.assign({}, this.state.milestonesValues);

    var objectSize = Object.keys(milestonesValues).length;
    var newInput = `milestone${objectSize}`;

    milestonesValues[newInput] = '';

    this.setState({ milestonesValues });
}

Check this example: 检查以下示例:

 let data = { milestonesValues : { milestone0: "dssdsad", milestone1: "", milestone2: "", milestone3: "", } }; function addelement(){ var milestonesValues = Object.assign({}, data.milestonesValues); var objectSize = Object.keys(milestonesValues).length; var newInput = `milestone${objectSize}`; milestonesValues[newInput] = ''; data.milestonesValues = milestonesValues; } addelement(); console.log(data.milestonesValues); 

var newInput = Object.assign({},
this.state.products.map(function(addNewfield){ addNewfield[val]= '' }));
this.setState({ products: newInput })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM