Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
JavaScript - How to access second object of an object

I need to access the last month data of an API, but I don't know how could I access the 2nd object, without hard coding the date like so: const data = [data.data['Monthly Time Series']['2021-11-30']]. In this example currently I would need the November data from the 'Monthly Time Series', but I need it to be dynamic instead of writing out the date, so it will always show the actual past month data. I would appreciate any help.

This is how the JSON looks like:

{
  "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 Respuestas
Responde la pregunta

0

This might look a little complicated but it definetely gives you the last date of your Monthly entries

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 Denunciar

0

Try

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

Will give the following result

[
  [
    '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'
    }
  ]
]

You can map through this list to fetch data of required date

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use Object.values(), to get an array of the Monthly Time Series values.

Once we have monthlyTimeSeries, we can get the second item using standard array notation ([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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda