2
  getCoinPrice(coinName: string) {
return this._http
  .get(
    `https://min-api.cryptocompare.com/data/pricemulti?fsyms=${coinName}&tsyms=EUR`
  ).pipe(map((result) => (result)));

JSON from the link with "BTC" as coinName is: {"BTC":{"EUR":8226.43}} and the method gives me back an observable.

How would I return the price value(8226.43) in a variable?

Quentin
  • 25
  • 4
  • 2
    change map to `map((result) => (result.BTC.EUR))` – Madhawa Priyashantha Jun 26 '20 at 13:08
  • (result) => (result)) in last part contains your json, if you change this last part to (result) => (console.log(result))) you will be able to see the value of response in console and there after you can access the object and properties of json as sugested by Madhawa Priyashantha – Arpita Jun 26 '20 at 13:25

3 Answers3

1

You want result[coinName]["EUR"]

Rahul Bhobe
  • 4,165
  • 4
  • 17
  • 32
1

Try this code. Should just work fine for you.

getCoinPrice(coinName: string) {
    return this._http
    .get(
      `https://min-api.cryptocompare.com/data/pricemulti?fsyms=${coinName}&tsyms=EUR`
  ).pipe(map((result) => (result.BTC.EUR)));

You can learn more about how to access objects and their value here https://www.w3schools.com/js/js_objects.asp

Here is the Javascript MDN explanation about how to work with objects: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

Working example:

This is to show how we will get the value you desired by accessing the objects with .

//Your results
var result = JSON.parse('{"BTC":{"EUR":8226.43}}');

//This will print out 8226.43
console.log(result.BTC.EUR)
Always Helping
  • 14,316
  • 4
  • 13
  • 29
  • This method gives me back an observable – Quentin Jun 26 '20 at 13:50
  • Can you please console.log(result) and see what coming in there? Please post it here. If the JSON output you had mentioned above then my answer should work just fine. – Always Helping Jun 26 '20 at 13:59
  • this is the output I get: Observable {_isScalar: false, source: Observable, operator: MapOperator} from using my edited code: .pipe(map((result) => (result.coinName.EUR))); – Quentin Jun 26 '20 at 14:14
  • Can you please just console.log like this: `.pipe(map((result) => (console.log(result))));` and post it here ? – Always Helping Jun 26 '20 at 21:34
  • @Quentin Can you please accept my solution as 'the' answer as well. Thanks :) – Always Helping Jun 29 '20 at 08:57
1

As per observable result you are getting {"BTC":{"EUR":8226.43}} for coinName BTC, you want to retrieve "8226.43" from it.

So, coinName = 'BTC' & observableResult = {"BTC":{"EUR":8226.43}}

If you want to get the value based on coinName, you could use the below method since coinName is also the key (BTC) in the observableResult object.

observableResult[coinName] // This will result to {EUR: 8226.43}

So, observableResult[coinName].EUR will result to 8226.43

aks44
  • 422
  • 3
  • 12
  • While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – ysf Jun 26 '20 at 19:14