1

I have the following: http://jsfiddle.net/Ve5ZQ/6/

I am attempting to reference the x value in the [x,y] pairs within chartGPS.series[0].data to determine the largest value of x currently in the series. However, it appears that I am referencing an undefined value for comparison.

'data' appears to be an array of arrays, so iterating through its pairs should be allowed with:

var lastUpdate = 0;
var theSeries = chartGPS.series[0].data;

// Loop to determine last updated timestamp (x value)
for (var i in theSeries) {
    // I think theSeries[i][0] should be the x value for each pair        
    alert(theSeries[i]); // Always alerts "undefined"             

    if (theSeries[i][0] > lastUpdate) {                
        lastUpdate = theSeries[i][0];
    }
}

What am I doing wrong?

Conor
  • 77
  • 8

3 Answers3

2

If you console.log(theSeries[i]); within your loop you can see that each index of the theSeries array has a property x:

for (var i in theSeries) {
    // I think theSeries[i][0] should be the x value for each pair
    alert(theSeries[i]);                
    if (theSeries[i][0] > lastUpdate) {                
        lastUpdate = theSeries[i][0];
    }
}

Changes to:

for (var i = 0, len = theSeries.length; i < len; i++) {
    console.log(theSeries[i]);                
    if (theSeries[i].x > lastUpdate) {                
        lastUpdate = theSeries[i].x;
    }
}

Here is a demo: http://jsfiddle.net/Ve5ZQ/8/

Here is a sample object in your array (each line is a different property, some properties have sub-properties):

-> Aa
--> _high: 809
--> category: 5326
--> clientX: 736.7
--> config: Array[2]
--> graphic: pa
--> plotX: 736.7
--> plotY: 55.4
--> pointAttr: Array[0]
--> series: c
--> x: 5326
--> y: 73
--> yBottom: null
--> __proto__: Object

This was copied from my console via the above JSFiddle, notice that it has a console.log(theSeries[i]) line inside the loop.

Jasper
  • 75,717
  • 14
  • 151
  • 146
  • Interesting. Thank you! Does that mean that theSeries[i] is an associative array or object created by the HC lib? Also, any suggestions for being able to more easily recognize this situation in the future? – Conor Mar 12 '12 at 22:48
  • @Conor **1.** `theSeries` is an array where each index is an object that holds several properties. **2.** Use `console.log()`, not `alert()`. Also use a browser that likes developers (basically not IE...) because the Chrome/Firefox developer tools seem to do a much better job than IE's native developer tools. When using `console.log()` you will be able to see all the data for an object/element. This is generally my first step with dealing with an object I didn't make myself, `console.log()` the object to see it's structure, then create my code based on the structure of the object. – Jasper Mar 12 '12 at 22:58
0

You should be using theSeries[i].x as theSeries[i] is an actual object with properies like x, y etc...

amit_g
  • 30,880
  • 8
  • 61
  • 118
  • Thanks. I got caught thinking that since series is defined as "series: [[],[],[]]", then the ['s indicate arrays. I'm assuming this is not always the case with javascript? What led you to notice that the x,y pairs were objects and not arrays of length 2? – Conor Mar 12 '12 at 22:52
0

See the answer by @CMS to the question Loop through array in JavaScript. It's very thoroughly explained there why for in should not be used with arrays.

Community
  • 1
  • 1
JamieSee
  • 12,696
  • 2
  • 31
  • 47