1

I have following html page with jquery script. Script is failing in IE8 at following line:

var dataCopy = JSONObject[event];
for (data in dataCopy)

error message I am getting is: "Object doesn't support this property or method". Same code works fine in Chrome and Firefox. Any suggestion would be great. I am trying to get all the distinct year values.

 !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <title>
          Google Visualization API Sample
        </title>
        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script src="..\Scripts\jquery-1.7.1.js" type="text/javascript"></script>
        <script type="text/javascript">
          google.load('visualization', '1', {packages: ['corechart', 'geochart', 'table'] });
        </script>
        <script type="text/javascript">
          function drawVisualization() {



            var JSONObject = {
                    cols: [{
                        id: 'Year', label: 'Year', type: 'string'
                    },{
                        id: 'data1', label: 'col2', type: 'number'
                    },{
                        id: 'data2', label: 'col3', type: 'number'
                    }],
                    rows: [{
                        c:[{v: '2000'}, {v: null}, {v: 3}]
                    },{
                        c:[{v: '2001'}, {v: 8}, {v:3 }]
                    },{
                        c:[{v: '2004'}, {v: 2.1244322}, {v: 4}]
                    }
                ]};

            var yearArray = [];

            for (var event in JSONObject) {
                var dataCopy = JSONObject[event];
                for (data in dataCopy) {
                    var mainData = dataCopy[data];
                    for (key in mainData) {
                        if (key.match(/c/)) {
                            var row = mainData[key];
                                yearArray.push(row[0].v);

                        }
                    }
                }
            }

            alert(yearArray.length);
}
</script>
2
  • 1
    dataCopy is not an object Commented Sep 26, 2013 at 14:16
  • var JSONObject = ... and JSONObject is not JSON, it's a JavaScript object. JSON is a textual, non-source-code notation. Commented Sep 26, 2013 at 14:33

2 Answers 2

2

Browsers other than IE8 support iterating over an array using the for...in iteration method. IE8 does not - it only supports iterating over objects in that manner.

I see you've got jQuery on your page so look at $.each - it will do what you want it to on both objects and arrays.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you! I have also tried using a simple for loop instead and it works too.
"Browsers other than IE8 support iterating over an array using the for...in...IE8 does not..." for-in does not loop through array entries. It loops through the enumerable properties of objects. It works on all standard JavaScript arrays (even in IE8 -- and 7, and 6) because those aren't really arrays, but may not work on host-provided arrays because they're not really JavaScript objects. for-in shouldn't be used for this in any case.
0

I created another array and used forloop to get around this problem. Like Adam suggested I could have used $.each function as well.

var dataCopy = [];

        for (var event in JSONObject) {
            dataCopy = JSONObject[event];
            for (var k=0; k < dataCopy.length; k = k+1) {
                var mainData = dataCopy[k];
                for (key in mainData) {
                    if (key.match(/c/)) {
                        var row = mainData[key];
                            yearArray.push(row[0].v);

                    }
                }
            }
        }

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.