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

130
Views
JavaScript: cómo acceder al segundo objeto de un objeto

Necesito acceder a los datos del último mes de una API, pero no sé cómo podría acceder al segundo objeto, sin codificar la fecha de la siguiente manera: const data = [data.data['Monthly Time Series']['2021-11-30']] . En este ejemplo, actualmente necesitaría los datos de noviembre de la 'Serie temporal mensual', pero necesito que sea dinámico en lugar de escribir la fecha, por lo que siempre mostrará los datos reales del mes pasado. Apreciaría cualquier ayuda.

Así es como se ve el JSON:

 { "Meta Data": { "1. Information": "Monthly Prices (open, high, low, close) and Volumes", "2. Symbol": "IBM", "3. Last Refreshed": "2021-12-08", "4. Time Zone": "US/Eastern" }, "Monthly Time Series": { "2021-12-08": { "1. open": "118.2500", "2. high": "123.3800", "3. low": "116.5600", "4. close": "123.0200", "5. volume": "33320654" }, "2021-11-30": { "1. open": "125.0500", "2. high": "127.2900", "3. low": "114.5600", "4. close": "117.1000", "5. volume": "119252012" },
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Esto puede parecer un poco complicado, pero definitivamente le da la última fecha de sus entradas mensuales.

 let data = { "Meta Data": { "1. Information": "Monthly Prices (open, high, low, close) and Volumes", "2. Symbol": "IBM", "3. Last Refreshed": "2021-12-08", "4. Time Zone": "US/Eastern" }, "Monthly Time Series": { "2021-12-08": { "1. open": "118.2500", "2. high": "123.3800", "3. low": "116.5600", "4. close": "123.0200", "5. volume": "33320654" }, "2021-11-30": { "1. open": "125.0500", "2. high": "127.2900", "3. low": "114.5600", "4. close": "117.1000", "5. volume": "119252012" } } } console.log( data["Monthly Time Series"] [Object.keys(data["Monthly Time Series"]) [Object.keys(data["Monthly Time Series"]).length - 1]]) // This gets you the last entry

about 4 years ago · Juan Pablo Isaza Report

0

Probar

 var result = Object.entries(data['Monthly Time Series']) console.log(result);

Dará el siguiente resultado

 [ [ '2021-12-08', { '1. open': '118.2500', '2. high': '123.3800', '3. low': '116.5600', '4. close': '123.0200', '5. volume': '33320654' } ], [ '2021-11-30', { '1. open': '125.0500', '2. high': '127.2900', '3. low': '114.5600', '4. close': '117.1000', '5. volume': '119252012' } ] ]

Puede mapear a través de esta lista para obtener datos de la fecha requerida

about 4 years ago · Juan Pablo Isaza Report

0

Podría usar Object.values() , para obtener una matriz de los valores de la serie temporal mensual.

Una vez que tenemos monthlyTimeSeries , podemos obtener el segundo elemento usando la notación de matriz estándar ([1]):

 let obj = { "Meta Data": { "1. Information": "Monthly Prices (open, high, low, close) and Volumes", "2. Symbol": "IBM", "3. Last Refreshed": "2021-12-08", "4. Time Zone": "US/Eastern" }, "Monthly Time Series": { "2021-12-08": { "1. open": "118.2500", "2. high": "123.3800", "3. low": "116.5600", "4. close": "123.0200", "5. volume": "33320654" }, "2021-11-30": { "1. open": "125.0500", "2. high": "127.2900", "3. low": "114.5600", "4. close": "117.1000", "5. volume": "119252012" } } } const monthlyTimeSeries = Object.values(obj["Monthly Time Series"]); const result = monthlyTimeSeries[1]; console.log('Result:', result)
 .as-console-wrapper { max-height: 100% !important; top: 0; }

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!