Holiday Notice: Support will be provided on a limited scale from December 24th, 2024, to January 2nd, 2025. Happy holidays and a wonderful New Year!


Topic: Select returns an array

master@mastersistemas.net pro asked 6 years ago


Hi, the select component returns an array, how can I get only the string?
getSelectAlterar= (value) => {

console.log(value);

varbancoAlterar=this.state.bancoAlterar;

bancoAlterar.nome=value;

this.setState({ bancoAlterar:bancoAlterar });

}
 
<Select getValue={this.getSelectAlterar}>

<SelectInputselected={this.state.bancoAlterar.nome}className="select"></SelectInput>

<SelectOptions>

<SelectOptionicon={brasil}>Banco do Brasil</SelectOption>

<SelectOptionicon={caixa}>Caixa</SelectOption>

<SelectOptionicon={carteira}>Carteira</SelectOption>

<SelectOptionicon={cresol}>Cresol</SelectOption>

<SelectOptionicon={viacredi}>Crevisc</SelectOption>

<SelectOptionicon={itau}>Itau</SelectOption>

<SelectOptionicon={santander}>Santander</SelectOption>

<SelectOptionicon={sicred}>Sicred</SelectOption>

<SelectOptionicon={sicoob}>Sicoob</SelectOption>

<SelectOptionicon={viacredi}>Viacredi</SelectOption>

<SelectOptionicon={outros}>Outros</SelectOption>

</SelectOptions>

</Select>
the console.log returns the selected item inside an array: Example: ["Cresol"] How can I get only one String?

Mikołaj Smoleński staff answered 6 years ago


Hi there, Please add value attribute to each SelectOption component. Then You should have a possibility to get value like I wrote above. Best Regards

Hi, good afternoon, I tried this way, but only give me the first letter. My code:
this.state = {

bancoAlterar:{agencia:"",ativo:0,ativodesc:"",codigo:"",conta:"",dv:"",id:0,nome:"",saldo:""},

}

getSelectAlterar = (Value) => {
var bancoAlterar =this.state.bancoAlterar;

bancoAlterar.nome =Value[0];

console.log(Value[0]);

this.setState({ bancoAlterar: bancoAlterar });

}

<Select getValue={this.getSelectAlterar}>

<SelectInput selected={this.state.bancoAlterar.nome}className="select"></SelectInput>
<SelectOptions>
<SelectOption icon={brasil}>Banco do Brasil</SelectOption>

<SelectOption icon={caixa}>Caixa</SelectOption>

<SelectOption icon={carteira}>Carteira</SelectOption>

<SelectOption icon={cresol}>Cresol</SelectOption>

<SelectOption icon={viacredi}>Crevisc</SelectOption>

<SelectOption icon={itau}>Itau</SelectOption>

<SelectOption icon={santander}>Santander</SelectOption>

<SelectOption icon={sicred}>Sicred</SelectOption>

<SelectOption icon={sicoob}>Sicoob</SelectOption>

<SelectOption icon={viacredi}>Viacredi</SelectOption>

<SelectOption icon={outros}>Outros</SelectOption>

</SelectOptions>

</Select>
Return of the console:
line:
thanks in advance.

Mikołaj Smoleński staff answered 6 years ago


Hi there, You can easily get string from an array. Here's the example:
console.log(value[0]);
Best regards

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Closed

Specification of the issue

  • ForumUser: Pro
  • Premium support: No
  • Technology: MDB React
  • MDB Version: 4.7.0
  • Device: Desktop
  • Browser: All
  • OS: All
  • Provided sample code: Yes
  • Provided link: No