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

91
Views
Cómo devolver una propiedad de objeto de una matriz de objetos en reaccionar

Tengo una matriz de objetos y necesito obtener una sola propiedad, pero no está definida y no tengo idea de por qué. ¿Alguien sabe cómo resolver?

 const [questions, setQuestions] = useState({}); const [index, setIndex] = useState(0); async function handleQuestions() { const fetchQuestions = await fetchTriviaApi(); setQuestions(fetchQuestions.results); } useEffect(() => { handleQuestions(); }, []);

Devolución de preguntas:

 (5) [{…}, {…}, {…}, {…}, {…}] 0: {category: 'Entertainment: Music', type: 'boolean', difficulty: 'medium', question: 'Rapper Snoop Dogg's real name is 'Cordozar Calvin Broadus, Jr.'.', correct_answer: 'True', …} 1: {category: 'Entertainment: Video Games', type: 'multiple', difficulty: 'medium', question: 'Which of these "Worms" games featured 3D gameplay?', correct_answer: 'Worms 4: Mayhem', …} 2: {category: 'Vehicles', type: 'multiple', difficulty: 'easy', question: 'What UK Train does NOT go over 125MPH?', correct_answer: 'Sprinter', …} 3: {category: 'Entertainment: Video Games', type: 'boolean', difficulty: 'medium', question: 'Tony Hawk's Pro Skater was released in 1999.', correct_answer: 'True', …} 4: {category: 'Entertainment: Video Games', type: 'boolean', difficulty: 'hard', question: 'In The Witcher 3, the Zoltan Chivay Gwent card can be found under the Hanged Man's Tree.', correct_answer: 'True', …} length: 5

Devolución de preguntas[índice]:

 {category: 'Entertainment: Music', type: 'boolean', difficulty: 'medium', question: 'Rapper Snoop Dogg's real name is 'Cordozar Calvin Broadus, Jr.'.', correct_answer: 'True', …}

Devolución de preguntas[índice].categoría: Cuando trato de acceder a cualquier propiedad, devuelve indefinido.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Debe inicializar el estado de las questions en una matriz vacía.

 const [questions, setQuestions] = useState([]);

Y al acceder a los elementos, debe verificar si las questions se completan con la llamada API.

 {questions.length > 0 && questions[index].category}
about 4 years ago · Juan Pablo Isaza Report

0

Lo más probable es que su búsqueda no se esté esperando correctamente. También debe esperar el json(). Y recuerda que setState es asíncrono y no inmediato. Solo en el próximo renderizado estará seguro de haber cambiado los datos.

about 4 years ago · Juan Pablo Isaza Report

0

Tal vez porque está inicializando el estado de sus preguntas como un objeto, por lo que las preguntas [1] no están definidas tan pronto como se resuelve handleQuestions.

about 4 years ago · Juan Pablo Isaza 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!