Home > Software design >  Set name in state along with its index in an iteration
Set name in state along with its index in an iteration

Time:09-29

I have an iteration code. I'm using react along with antd select.

      {this.state.count.map((d, i) => (
          <Select
            placeholder={`Question #${i   1}`}
            style={{ width: '100%' }}
             onChange={(e) => this.setState({securityQuestions{i}:e})} // here I have issue.. need to set name with index
             showSearch
             optionFilterProp="children"
             filterOption={(input, option) =>                                                    
    option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
         filterSort={(optionA, optionB) =>              
    optionA.children.toLowerCase().localeCompare(optionB.children.toLowerCase())
                                                                    }>  
{this.state.securityQuestions && this.state.securityQuestions.map((d) => <Select.Option value={d.id}>{d.securityQuestion}</Select.Option>)}
                 </Select>
         ))}

My code is like this. As i mentioned in code I have issue in 'onchange'. I want to set name along with its index. Can anyone help me?

CodePudding user response:

If I understand correctly, your goal is to end up with something like this?:

{securityQuestions1:e}

In that case you can use bracket notation to reference the property with a value, and then your value is just an interpolated string (template literal):

{[`securityQuestions${i}`]:e}

Or if you prefer a concatenated string:

{['securityQuestions'   i]:e}
  • Related