What is the best way to extract numerical data from the second object in a JSON file?

How can I retrieve the data from the second object's tts number (1,152.81) and display it correctly?

I attempted to use priceTag.innerHTML = data[0].tts in my javascript file below, but it doesn't seem to be working as expected. Can you help me figure out how to access that specific data?

const url = "https://www.koreaexim.go.kr/site/program/financial/exchangeJSON?authkey=KiGkSEG4nhXCrQ0PioaqVQJDb0lEfKeE&data=AP01"
const priceTag = document.querySelector("h1")

// fetch data
const checkPrice = function() {
  fetch(url)
    .then(response => response.json())
    .then(data => {
      priceTag.innerHTML = data[0].tts
    })
}

checkPrice()

The JSON file contains a total of 22 objects, but I have only included 2 of them for reference.

[
{
"result": 1,
"cur_unit": "THB",
"ttb": "35.42",
"tts": "36.13",
"deal_bas_r": "35.78",
"bkpr": "35",
"yy_efee_r": "0",
"ten_dd_efee_r": "0",
"kftc_bkpr": "35",
"kftc_deal_bas_r": "35.78",
},
{
"result": 1,
"cur_unit": "USD",
"ttb": "1,129.98",
"tts": "1,152.81",
"deal_bas_r": "1,141.4",
"bkpr": "1,141",
"yy_efee_r": "0",
"ten_dd_efee_r": "0",
"kftc_bkpr": "1,141",
"kftc_deal_bas_r": "1,141.4",
}
]

Answer №1

let exchangeRates = [{
    "result": 1,
    "cur_unit": "THB",
    "ttb": "35.42",
    "tts": "36.13",
    "deal_bas_r": "35.78",
    "bkpr": "35",
    "yy_efee_r": "0",
    "ten_dd_efee_r": "0",
    "kftc_bkpr": "35",
    "kftc_deal_bas_r": "35.78",
  },
  {
    "result": 1,
    "cur_unit": "USD",
    "ttb": "1,129.98",
    "tts": "1,152.81",
    "deal_bas_r": "1,141.4",
    "bkpr": "1,141",
    "yy_efee_r": "0",
    "ten_dd_efee_r": "0",
    "kftc_bkpr": "1,141",
    "kftc_deal_bas_r": "1,141.4",
  }
]

console.log(exchangeRates[1].tts)

Answer №2

Have you tested using data[1]? Your current data is an array where each element is an object.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using accent marks in JSON manifest files

I am trying to add a description in the manifest.json file: "description" : "Somos la única empresa agroindustrial del país presente en toda la cadena de valor granaria." However, I have encountered an issue where the accents are displayed incorrectly ...

Shadows in THREE.JS are not being affected by the displacement map

Lately, I've been facing some challenges with getting the shadows to work properly when using a displacement map. Despite researching the issue on Google, I still can't seem to figure it out. From what I gather, the displacement map is supposed t ...

What could be causing my Ajax request with dataType set to "jsonp" to encounter errors?

My code includes an Ajax call that looks like this: var baseurl = Office.context.mailbox.restUrl; var getMessageUrl = baseurl + "/v2.0/me/messages/" + rest_id + "?$select=SingleValueExtendedProperties&$expand=SingleValueExtendedPropertie ...

Utilize the power of JavaScript to recursively map object keys

I am working with an array of objects that have varying depths. My goal is to output the names in a specific format: Top Level Top Level > Sub 1 Top Level > Sub 1 > Sub 1-2 Top Level > Sub 2 Unfortunately, I am only able to get the name of th ...

What is the process for choosing and making changes to a specific portion of a textContent?

<h1 id="block ">This is my header block</h1> To dynamically change the color of the "block" word every 2 seconds using JavaScript: let colors = ["blue", "green", "pink", "purple"]; let curColor = 0; const changeColor = () => { ...

The accumulation of classes occurs during the cloning process of a template used for a data array

Encountered an issue while duplicating a template div to generate elements for a dataset. The problem arises from classes stacking up when creating elements for each data entry. Sample JavaScript code: $(document).ready(function(){ var data = [ { ...

Creating a dynamic table accordion with PHP and MySQL

I am currently working on creating an accordion table to display data retrieved from a database. I want the description data to be shown in a row below when clicking on the respective row. Despite my efforts in modifying various code snippets that I have c ...

What is the reasoning behind an empty input value being considered as true?

I am facing an issue with the following code that is supposed to execute oninput if the input matches the answer. However, when dealing with a multiplication problem that equals 0, deleting the answer from the previous calculation (leaving the input empt ...

How to give focus to a div in IE 8 after pressing the tab key, no jQuery required

We are facing a challenge with our datagrid where we have implemented navigation using the tab key. In IE 7 & 8, pressing the tab key shifts the focus away from the grid to the next element on the page. While in other browsers, we were able to prevent thi ...

Missing data: Node JS fails to recognize req.body

I've looked through various posts and I'm feeling quite lost with this issue. When I run console.log(req), the output is as follows: ServerResponse { ... req: IncomingMessage { ... url: '/my-endpoint', method: &a ...

Creating a polygon path in Google Maps v3 using an array of coordinates

I'm currently working on creating a polygon around US counties. The coordinates for this polygon are coming from a database and then being json encoded for use in JavaScript. Below is the JSON encoded array generated from PHP code: {"Abbeville-sc":[[ ...

Tips for organizing an array with mixed data types using JavaScript

I'm still learning JavaScript, and I'm struggling with sorting an array. Let's say I have two arrays like this: var mergedArray = result.Entities.concat(result.NonClickablePaths); var allPaths = result.AllPaths; The data in both arrays look ...

Using console.log() within a method while chaining in JavaScript/jQuery

I've been experimenting with developing jQuery plugins and I'm interested in chaining methods. The jQuery tutorial (found here: https://learn.jquery.com/plugins/basic-plugin-creation/) mentions that you can chain methods by adding return this; at ...

When trying to implement a MYSQL stored procedure, I encountered an issue where the condition

I am currently working on creating a JSON object from a MySQL select statement and passing it to an output parameter. Everything seems to be functioning correctly, except for the fact that my WHERE condition is not being applied when I use json_object, jso ...

Begin an anonymous function once the AJAX request has been completed

Greetings! I am currently working on a template where the scripts are initialized in the file as shown below: ;(function ($) { "use strict"; var $body = $('body'); var $head = $('head'); var $header = $('#header'); var tran ...

Modifying paragraph content with JavaScript based on selected radio button values and troubleshooting the onclick event not triggering

I am working on implementing a language selection feature on my website where users can choose between English and Spanish. The idea is to have two radio buttons, one for each language, and a button. When the button is clicked, the text of the paragraphs s ...

Automatically unset session variable 'unsetted' using a simple line of code

Why does the session information not get saved? When I reload the page, the session variable 'mucciusersess' disappears. What could be causing this issue? Thanks... I have a cookie on the client-side with a value like 'mucciuserid' se ...

Issue encountered with Tesseract.js: Unable to run 'toBlob' function on 'HTMLCanvasElement': Exporting tainted canvases is not allowed

Currently using Chrome and a server set up using node. Below is the HTML code: <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie= ...

Guide on how to combine the strings retrieved from an API

I'm having trouble concatenating multiple sentences together and highlighting specific words in each sentence. Although I can successfully highlight one sentence using the code below, I haven't been able to concatenate more than one sentence: th ...

Using EditTexts within a GridView: Effective ways to retrieve values from multiple EditText fields

I have utilized the following XML code to create a GridView: grid_layout.xml <GridView android:id="@+id/productList" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="5dp" android:layout_ ...