1

I'm having a hard time selecting the price_change of this array.

I tried like this but it doesn't work:

response.data[0].[1d].price_change

Any help is appreciated!

enter image description here

    var stockDiff = document.getElementById("stockDiffCrypto");
    var thePrice = response.data[0].["1d"].price_change;

if (thePrice > 0) {
    stockDiff.innerHTML = `<i class='fas fa-angle-double-up'></i>`
    stockDiff.style.color = "green"
} else {
    stockDiff.innerHTML = `<i class='fas fa-angle-double-down'></i>`
    stockDiff.style.color = "red"
}
3
  • Are you getting an error in the console? What happens if you put quotes around "1d": response.data[0].["1d"].price_change? The way you have it now, it's probably trying to evaluate the variable 1d. Commented Oct 7, 2021 at 19:59
  • I'm getting console error: (Uncaught SyntaxError: missing name after . operator). As well in the script, after response.data[0]. I'm getting error TS1003: (JS) identifier expected Commented Oct 7, 2021 at 20:00
  • I just fixed. var thePrice = response.data[0]["1d"].price_change; Commented Oct 7, 2021 at 20:06

1 Answer 1

2

variable.["property"] is not valid syntax. These 2 are valid:

variable.property
variable["property"]

This is the same with arrays

array[0][0]

In your case it should be this

var thePrice = response.data[0]["1d"].price_change
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.