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

278
Views
Agregar elementos a la matriz desde JSON está devolviendo una matriz vacía. ¿Cómo puedo detener esto?

Tengo un archivo .JSON, he leído los datos y estoy usando un bucle for...in para recorrerlo.

Si uso console.log , puedo imprimir los datos en la consola, pero si trato de agregar datos a una matriz usando la función push y console.log la matriz, obtengo una matriz vacía.

Básicamente, ¿cómo agrego los datos a la matriz?

El código:

 let wordList = [] fetch("wordleanswers.json") .then(response =>response.json()) .then(data=>{ let myWord = data.toString() for (const singleData in data){ wordList.push(singleData) } }) console.log(wordList)

¿Se me escapa algo?

EDITAR: aquí hay una versión .txt del archivo .json (el texto está solo en un objeto en .json): https://github.com/MissingFable/wordle_bot_4_python/blob/main/wordleanswers.TXT

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

0

Está llamando a un código asíncrono para obtener los datos. Debe esperar en la recuperación antes de registrar su matriz.

Trate de usar then al final. Como abajo.

 let wordList = [] fetch("wordleanswers.json") .then(response =>response.json()) .then(data=>{ let myWord = data.toString() for (const singleData in data){ wordList.push(singleData) } }).then(() => { console.log(wordList) })

O puede poner todo este código en la función asíncrona y await antes de llamar a la función de búsqueda.

Editar: Agregar la otra solución (usando la función IIFE)

 (async () => { let wordList = [] const response = await fetch("wordleanswers.json") const data = await response.json() for (const singleData in data){ wordList.push(singleData) } console.log(wordList) })()
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!