0

My Code :

for( var i in zones) {
                    var latlons1 = new google.maps.LatLng(zones[i].b.cen_x, zones[i].b.cen_y);
                    var latlons2 = new google.maps.LatLng(zones[i].b.max_x, zones[i].b.max_y);
                    var latlons3 = new google.maps.LatLng(zones[i].b.min_x, zones[i].b.min_y);

                     obj1 = { zones[i].n = {1:latlons1,2:latlons2,3:latlons3}  } //here object creation

                    console.log(obj1);

                }

what i am doing wrong? consol log error shows at object create.

4
  • 2
    you're not properly using objects on that obj1 line. Commented Oct 23, 2012 at 11:59
  • thanks for answer, please show me how to use object at this moment Properly Commented Oct 23, 2012 at 12:01
  • If you are using numbers as index, why are you not using an array to start? Commented Oct 23, 2012 at 12:30
  • @Dest this is nothing close to a tutorial website. Commented Oct 23, 2012 at 22:36

2 Answers 2

6

When creating an object literal in JavaScript the key and value are separated by a colon (:). Also note that if you want to use dynamic keys then you'll need to use the square bracket notation for setting and accessing those properties. So this:

obj1 = { zones[i].n = {1:latlons1,2:latlons2,3:latlons3}  }

should become:

obj1 = {};
obj1[zones[i].n] = {
    1: latlons1,
    2: latlons2,
    3: latlons3
};

If you're confused as to why you have to do it this way it's because the keys aren't evaluated. While you meant that the key should be the value that's referenced by zones[i].n, JavaScript interprets it as the key should be the string literal "zones[i].n", which obviously isn't what you want.

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

1 Comment

Yes, in that case zones[i].n will be the top level key which contains an object with three props.
0

To use an object within an object,

    //An object within an object
    var NestedObject=new Object();
    NestedObject.one={
    sad:{
    d:"Add"
    }
    }

I solved this using experimental coding within Codecademy.com; Thanks for letting me share my answer!

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.