0
hi = [ { 0: { symbol: "asdf" , name:"adad"} }]

How will you access symbol property here in JS

console.log(hi[0]) outputs { symbol: "asdf" , name:"adad"} but console.log(hi[0].symbol) throws the error that symbol is undefined

ROOT
  • 11,363
  • 5
  • 30
  • 45
Tarun Bisht
  • 121
  • 13

4 Answers4

1

hi is an array. First you need to access the object so hi[0] will provide the first object. Then access the object using the key which is 0 like below

const hi = [{
  0: {
    symbol: "asdf",
    name: "adad"
  }
}];

console.log(hi[0][0].symbol)
brk
  • 48,835
  • 10
  • 56
  • 78
1

hi variable is an Array so you have to access the first element of the array first, then the Object property:

hi = [ { 0: { symbol: "asdf" , name:"adad"} }]

console.log(hi[0][0].symbol)
ROOT
  • 11,363
  • 5
  • 30
  • 45
0

Like this?

console.log(hi[0][0].symbol)
Marco
  • 7,007
  • 2
  • 19
  • 49
  • Cannot read property '0' of undefined – Tarun Bisht May 14 '20 at 04:20
  • 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. – SherylHohman May 15 '20 at 00:40
0
hi = [ { 0: { symbol: "asdf" , name:"adad"} }]

hi[0][0].symbol is right.

console.log(hi[0]) => 0: {symbol: "asdf", name: "adad"}

console.log(hi[0][0]) => {symbol: "asdf", name: "adad"}

console.log(hi[0][0].symbol) => "asdf"
Jordan Micle
  • 59
  • 1
  • 8
  • 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. – SherylHohman May 15 '20 at 00:52