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

125
Views
Acceda al valor más reciente en el objeto anidado JSON con JS

Estoy tratando de acceder al elemento más reciente en el objeto Time Series (5 min) , sin tener que especificar la fecha/hora, después de usar este código JS:

 var getStock = new XMLHttpRequest(); getStock.open("GET","https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo", false); getStock.send(null); var current_stock = JSON.parse(getStock.responseText); console.log(current_stock); var current_stock_price = current_stock["Time Series (5min)"][0]["4. close"];

Entonces, en este caso (ver captura de pantalla) es Time Series (5 min) > 2022-04-21 20:00:00 -> 4. close , pero aparece un error indefinido.

Incluso probé en la consola del desarrollador con el archivo JSON completo. Usar current_stock["Time Series (5 min)"] devuelve todos los valores secundarios en la consola, pero agregar [0] o ["2022-04-21 20:00:00"] al final arroja un error indefinido.

JSON sin procesar

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puedes acceder a él así:

 var getStock = new XMLHttpRequest(); getStock.open("GET", "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo", false); getStock.send(null); var current_stock = JSON.parse(getStock.responseText); const timeSeries = current_stock['Time Series (5min)'] const key = Object.keys(timeSeries)[0] console.log(timeSeries[key]['4. close'])

about 4 years ago · Santiago Trujillo Report

0

Cuando cambio [0] a ["2022-04-21 20:00:00"], como usted sugirió, funciona bien. El código será entonces:

 var getStock = new XMLHttpRequest(); getStock.open("GET","https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo", false); getStock.send(null); var current_stock = JSON.parse(getStock.responseText); var current_stock_price = current_stock["Time Series (5min)"]["2022-04-21 20:00:00"]["4. close"]; console.log(current_stock_price)

Si no desea usar las claves, puede usar Object.values() para acceder a los datos:

 var getStock = new XMLHttpRequest(); getStock.open("GET","https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo", false); getStock.send(null); var current_stock = JSON.parse(getStock.responseText); var current_stock_price = Object.values(current_stock["Time Series (5min)"])[0]["4. close"]; console.log(current_stock_price)

Esto devolverá la última entrada.

about 4 years ago · Santiago Trujillo Report

0

Esto se debe a que current_stock["Time Series (5min)"] es un objeto. No es una matriz, por eso el índice 0 no está disponible. Si desea acceder al primer elemento en current_stock["Time Series (5min)"], aquí hay algo que puede hacer

 var getStock = new XMLHttpRequest(); getStock.open("GET","https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo", false); getStock.send(null); var current_stock = JSON.parse(getStock.responseText); console.log(current_stock); var keys = Object.keys(current_stock["Time Series (5min)"]); // return all the keys in current_stock["Time Series (5min)"] object console.log(keys) // keys is array, so you can access first item as keys[0] var current_stock_price = current_stock["Time Series (5min)"][keys[0]]["4. close"]
about 4 years ago · Santiago Trujillo 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!