Retrieve precise information using JavaScript fetch

Seeking guidance on how to extract specific data using JavaScript fetch. Currently working with the following API: . I am struggling to select just one currency, as my forEach loop displays all currencies but I want to display only one. To show all currencies, I have been using:

${data.rates[rate].name}
${data.rates[rate].rate}

In an attempt to display just one currency, I have tried multiple approaches, but this one seems most logical:

${data.rates[rate].name == dkk}
${data.rates[rate].rate}

However, this does not work. Does anyone know how to specify and display only a single set of data?

Answer №1

${data.rates[rate].name == dkk}

It appears that the ${} syntax used here is for JavaScript string interpolation. The expression being evaluated here is whether the value of .name is equal to dkk. This likely results in displaying either "true" or "false.

If you aim to display the name only if it matches dkk, you can achieve this by first defining a variable like so:

let displayName = '';
if (data.rates[rate].name === dkk) {
    displayName = data.rates[rate].name;
}

Then you can interpolate ${displayName} to achieve the desired output.

If this isn't the intended functionality, additional code context would be needed for further assistance.

Answer №3

  fetch('https://apiv2.bitcoinaverage.com/constants/exchangerates/global')
   .then(response => response.json())
   .then(result => result.rates.AED)
   .then(console.log)

By using the fetch method, a Promise is returned allowing you to work with the data using .then. In the provided code snippet, .then is utilized to extract and log the exchange rate for United Arab Emirates (AED) from the fetched data. I trust this explanation clarifies the process.

Answer №4

Hey there, I'm the original poster! I thought it might be helpful to share how I resolved this issue for anyone who comes across it later on:

rates.forEach(currency=>{
 if(data.rates[currency].name == 'Danish Krone'){ //if currency name == 'something'
 output = output +
 `
 ${data.rates[currency].name}
 ${data.rates[currency].rate}<br>
 `
}                    
})

Essentially, I learned that you can't simply write ".name == dkk" to retrieve the currency's name. Instead, I had to create an if statement to specifically check if the name matches the desired 'currency name'. Additionally, in this particular API, I couldn't use the abbreviated version (e.g., DKK) and had to input the full name in the if statement. Once I made those adjustments, obtaining the name and rate of the currency followed the same process as looping through all the rates. Hopefully this explanation proves useful to someone down the line who finds themselves in a similar situation :)

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

Are you facing a problem with the navigation buttons for playing the next and previous

After setting the title of the video, it initially appears for a brief moment but then disappears once each video has finished playing. Interestingly, everything functions as expected when the play-next/prev buttons are not activated. However, if I rapid ...

JavaScript & PHP: Encoding and Decoding Techniques

I am encountering an issue where only half of the HTML content is being retrieved when I send it using the POST method in Ajax and try to retrieve it using PHP. The content includes special characters. For example, I am posting the following content to an ...

In React, my unmanaged radio group only triggers the form's onChange event once for each radio element

UPDATE: There seems to be a bug in React that is affecting version 15.6.1 but not version 15.1.0. You can find more information about this issue at https://github.com/facebook/react/issues/10078 The problem I'm encountering is related to adding a cha ...

A guide on verifying the occurrence of a specific event on an element with Jquery

I have a server control with four buttons that do not have IDs, but each pair of buttons has two CSS classes assigned to them. I want to determine if the click() event exists for these buttons based on their class as an identifier. To retrieve the list of ...

Ways to incorporate a unique debounce technique where the function is only executed every nth time

const _debounce = (num, fn) => { //implementation goes here } const originalFunction = () => { console.log('fired') } const _callFunc = () => _debounce(2, originalFunction) _callFunc() //The originalFunction does not run _callFun ...

Creating 3D Shapes with three.js

I am currently in the process of importing an STL object into my three.js scene. Unfortunately, this particular object seems to be using a large amount of GPU resources for rendering and animation, causing the overall performance of the scene to suffer. B ...

Using Rxjs to dynamically map values from an array with forkJoin

Greetings! I have a collection of Boolean observables and would like to apply a logical AND operation. Currently, I am passing static values 'a' and 'b', but I am unsure of the number of elements in the totalKeys array. import { forkJoi ...

What steps should I take to resolve the "StrictMode has found deprecated findDOMNode" error?

Whenever I trigger the button to open the drawer, my console displays the warning message '' findDOMNode is deprecated in StrictMode'' The container for the button component is called Sidenav import Sidenav from './Sidenav'; ...

Customer is unable to locate the "InitializeAuthenticationService" function

After launching my application, the browser console keeps showing me three errors that all say Could not find 'AuthenticationService.init' ('AuthenticationService' was undefined). and Microsoft.JSInterop.JSException: Could not find &apo ...

Instructions on replicating the content from one div to another div

Is there a way to exhibit the modified content of div (container1) in another div (container2)? <div id="container1"> <div id="slide1"> Here, you will find various elements such as input boxes, select dropdowns, and plain text. </div& ...

The getelementbyid function is unable to locate the specified button identifier

As I dive into the world of Javascript, I wanted to create a simple input form with a corresponding response field on my website. However, I encountered an issue where using a basic submit button caused the page to refresh and erase the textfields before ...

Updating numerous records with varying values

Utilizing jQuery UI's sortable feature (source) allows me to rearrange elements effortlessly. By implementing custom callbacks, I can generate a list of these elements, assigning each a new position ID as I move them around. The resulting list may app ...

Are there any web browsers that automatically switch to a non-SSL connection if an attempt to connect with SSL

I regularly utilize jQuery along with jQuery.ajax to make connections between pages. I am interested in establishing a connection from a non-SSL page to an SSL page using ajax. Are there any web browsers that will attempt to connect via non-SSL if the con ...

Managing global devDependencies with Node and npm: A comprehensive guide

As I develop a Node module, I have included devDependencies like jasmine-node and jshint that need to be globally installed. My goal is to access their binaries in my makefile / npm scripts without using require(). Despite my research, I'm still unsu ...

Activating an HTML button based on a specific condition

One of the challenges I faced was with two HTML buttons that I have set up: <button id="alarm-log-submit" class="btn btn-primary alarm-log-btn" >Generate Log</button> <button id="alarm-log-download" class="btn btn-primary alarm-log-second-b ...

Mapping two arrays in JavaScript involves iterating through each element of the arrays

I'm having trouble displaying the content of two arrays contained within an object. When I map over RType.information.Type, I can display the content of the "Type" array. However, I am unable to display both Type[] and Price[]. I've tried various ...

What is the best way to showcase a single item from an array in Vue.JS?

Suppose I have a list of items in Vue.JS data () { return{ items: [ { itemID: 5, itemText: 'foo' }, { itemID: 3, itemText: 'bar' } ] } } Now, if I want to show the item with the itemID of 3 i ...

Unexpected dependency mysteriously appeared in package.json during npm install

I'm in the process of developing a project using npm 7.24.0 and executing npm install --lockfile-version 2 --legacy-peer-deps After the project is built, it adds an additional line to package.json "dependencies": { "2": &quo ...

What steps should I take to create a collapsible Bootstrap navbar?

I'm attempting to recreate the scrolling functionality seen here: It seems like they might be using a customized Bootstrap Navbar, so I've taken one from here: and tailored it to my needs. How can I achieve the effect where the navigation bar ...

Determining the coordinates of an object post-rotation using three.js

After applying a rotation to my object, I want to retrieve its new position in order to move my camera there and set the lookAt vector accordingly. Can anyone provide guidance on how to achieve this? Thanks in advance! ...