1

I want to get the first object as given in the image- image

But the array length is showing zero. But when I run typeof it shows object. It is an array of object or something else? This is the code to get the first object

(this.trendChart.yAxis[0]['series'])[0]

EDIT
I want the first object from series array - image

EDIT
On using (this.trendChart.yAxis[0]['series']) i'm getting the value as shown in first image.

Rebecca
  • 45
  • 8
  • `typeof` always says `object` for arrays. – Barmar Apr 20 '21 at 19:40
  • 2
    Are you filling the array using an async function? You're probably checking the length before it has completed. – Barmar Apr 20 '21 at 19:41
  • See https://stackoverflow.com/questions/23667086/why-is-my-variable-undefined-after-i-modify-it-inside-of-a-function-asynchron – Barmar Apr 20 '21 at 19:42
  • Your question generally needs more detail. The length of which array is zero? What value are you running `typeof` against, an array or an element of the array? – backtick Apr 20 '21 at 19:42
  • @backtick typeof this.trendChart.yAxis[0]['series'] gives object. – Rebecca Apr 20 '21 at 19:45
  • Probably best to update the question with this context if you get a chance. Barmar's comment here is relevant. `typeof` for any array is always `'object'`, it doesn't matter if the array is empty or not. – backtick Apr 20 '21 at 19:47
  • I was just checking what is the type. I want to get the first object. @Barmar – Rebecca Apr 20 '21 at 19:59
  • As I said above, you're probably trying to get the first object before the async function has assigned it. – Barmar Apr 20 '21 at 20:01
  • But you're viewing it in the console after it has been updated. The console has a live link to the array, so it updates as the array is filled in. See https://stackoverflow.com/questions/34277332/javascript-console-log-reporting-object-properties-incorrectly – Barmar Apr 20 '21 at 20:04
  • Hi @Shawn, Could you reproduce the problem in some online code editor? – ppotaczek Apr 21 '21 at 18:29
  • You can simulate async operations for example by using `setTimeout`. – ppotaczek Apr 22 '21 at 09:40
  • can you please help in this highcharts stock tool problem. [link](https://stackoverflow.com/q/67311156/15500072) @ppotaczek – Rebecca Apr 29 '21 at 06:12

1 Answers1

1

Showing us the whole object structure in text instead of an image would help. From your image, try this.trendChart.yAxis.series[0].

cMarius
  • 1,090
  • 2
  • 11
  • 27
  • 1
    try showing us the whole structure of the `this.trendChart` object in text – cMarius Apr 20 '21 at 19:59
  • 1
    `this.trendChart.yAxis[0].series[0]` is equivalent to what they used in the code. – Barmar Apr 20 '21 at 20:02
  • that's true, and it's highly it's possible you're right and he's using that variable before it's initialized properly, but he mentioned that `typeof` shows it as an object so it might just be a syntax issue. In any case the only way to know is if we see the whole object. – cMarius Apr 20 '21 at 20:06