evll
evll

Reputation: 129

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"
    },

Upvotes: 1

Views: 1370

Answers (3)

Terry Lennox
Terry Lennox

Reputation: 30685

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; }

Upvotes: 2

Branchverse
Branchverse

Reputation: 1397

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

Upvotes: 2

Shashikamal R C
Shashikamal R C

Reputation: 522

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

Upvotes: 1

Related Questions