Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
Quiero hacer un bucle en una matriz en el estado y verificar si la entrada existe en él

Traté de construir este método addgamer() para verificar si el nombre de usuario existe en la matriz de jugadores al hacer clic en el botón Agregar y, si es así, quiero que no agregue su información como un objeto en la matriz, quiero saber por qué no no funciona? ....creé este método enterdata() para obtener los valores de estos tres jugadores al cambiar los valores de sus entradas

 class App extends Component { state= { gamer:{ FirstName:"", LastName: "", UserName: "", }, gamers:[], } enterdata = (event)=> { const {name, value} = event.target; this.setState((prevstate)=>({ gamer: {...prevstate.gamer,[name] : value}, })) } addgamer = (event) =>{ event.preventDefault(); for(let i = 0;i<this.state.gamers.length; i++){ if(this.state.gamer.UserName === this.state.gamers[i].UserName){ return false; }else{ this.setState(prevstate =>({ gamers: [...prevstate.gamers, this.state.gamer], gamer: {FirstName:"", LastName: "", UserName: ""}, })) } } }
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

El problema aquí es que estás agregando al jugador cada vez que no se encuentra el nombre de usuario. Lo que debe hacer es establecer una marca para ver si el nombre de usuario se encontró en la lista de jugadores o no, y luego hacer setState para agregar el nuevo jugador si no se encontró el nombre de usuario, así:

 let found = false; for(let i = 0;i<this.state.gamers.length; i++){ if(this.state.gamer.UserName === this.state.gamers[i].UserName){ found = true; break; } } // alternatively: let found = this.state.gamers.find(gamer => this.state.gamer.UserName === gamer.UserName) if (!found) { this.setState(prevstate =>({ gamers: [...prevstate.gamers, this.state.gamer], gamer: {FirstName:"", LastName: "", UserName: ""}, })) }
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!