1

I'm passing an object of a certain type to an array. When I print the object from an outside reference I can see all the fields. When I print the same object from the array reference, I see almost none.

To Explain, here is the code that illustrates the problem:

var test = new OpenLayers.Layer.Vector();

var array = [test];

console.log("******* Printing Test directly ***************");
printObjectProperties(test);

console.log("****** Printing Test from array **************");
for (obj in array){
    printObjectProperties(obj);     
}

The printObjectProperties() function:

function printObjectProperties(eventData) {
        for (var prop in eventData) {
            if (!(typeof (eventData[prop]) == "undefined")) {
                console.log(prop + " = " + eventData[prop]);
            }
        };
}

The complete code (complete html file, you can run and see for youself in the browser console log): (live example)

<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
<script>

    $(document).ready(function(){

        var test = new OpenLayers.Layer.Vector();

        var array = [test];

        console.log("******* Printing Test directly ***************");
        printObjectProperties(test);

        console.log("****** Printing Test from array **************");
        for (obj in array){
            printObjectProperties(obj);     
        }

    });


function printObjectProperties(eventData) {
        for (var prop in eventData) {
            if (!(typeof (eventData[prop]) == "undefined")) {
                console.log(prop + " = " + eventData[prop]);
            }
        };
}

</script>
</html>
6
  • It is more helpful if you create a jsFiddle demo. I made one: jsfiddle.net/tu6er Commented Aug 8, 2011 at 11:23
  • 1
    Thanks. I actually created a demo, search for "live example" Commented Aug 8, 2011 at 11:24
  • :) Must have missed that... ;) Commented Aug 8, 2011 at 11:25
  • Why don't you just pass the object/array to console.log instead of trying to format it yourself? Try this and you'll see all the properties: jsbin.com/evazip/3/edit#javascript,html Commented Aug 8, 2011 at 11:30
  • 1
    @Ben: for...in is used to loop over properties of objects. It is much slower than a for loop. For more reasons to not use it for arrays, have a look at developer.mozilla.org/en/JavaScript/Reference/Statements/… Commented Aug 8, 2011 at 14:13

2 Answers 2

3

In a for in loop, the first variable (obj) is the key. In an array that's the index.

So basically you're iterating over a number in printObjectProperties. Hence the 0 = 0, because the first index of your array (you have one element) is 0.

Use this instead:

printObjectProperties(array[obj]);

which would evaluate to:

printObjectProperties(array[0]);

or:

printObjectProperties(test);
Sign up to request clarification or add additional context in comments.

Comments

1

You don't iterate over an array using for in:

for (var i = 0, len = array.length; i < len; ++i) {
    console.log(array[i]);
}

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.