When I console.log
my data, it looks like this in Chrome Dev Tools:
e.LatLngBounds {_southWest: e.LatLng, _northEast: e.LatLng}
_northEast: e.LatLng
lat: 50.62507306341435
lng: -69.169921875
__proto__: Object
_southWest: e.LatLng
lat: 27.059125784374068
lng: -127.96875
__proto__: Object
__proto__: Object
If I JSON.stringify
it (like so: var totbounds = JSON.stringify(mybounds);
), I get this:
{"_southWest":{"lat":27.059125784374068,"lng":-127.96875},"_northEast":{"lat":50.62507306341435,"lng":-69.169921875}}
I'm trying to parse it to get the _southWest
lat
(for example), but neither of these work:
totbounds[_southWest][lat];
totbounds._southWest.lat;
(Note, totbounds
is the stringified object)
When I console.log
my data, it looks like this in Chrome Dev Tools:
e.LatLngBounds {_southWest: e.LatLng, _northEast: e.LatLng}
_northEast: e.LatLng
lat: 50.62507306341435
lng: -69.169921875
__proto__: Object
_southWest: e.LatLng
lat: 27.059125784374068
lng: -127.96875
__proto__: Object
__proto__: Object
If I JSON.stringify
it (like so: var totbounds = JSON.stringify(mybounds);
), I get this:
{"_southWest":{"lat":27.059125784374068,"lng":-127.96875},"_northEast":{"lat":50.62507306341435,"lng":-69.169921875}}
I'm trying to parse it to get the _southWest
lat
(for example), but neither of these work:
totbounds[_southWest][lat];
totbounds._southWest.lat;
(Note, totbounds
is the stringified object)
-
3
If you stringify the object, you get a JSON string, not an object. You should read the properties from the object, not the string. Use
mybounds._southWest.lat
. – Guffa Commented Jun 11, 2015 at 1:04 - Try this: jsbin./lumeqemaza/1/watch?js,console – Chan Commented Jun 11, 2015 at 1:04
-
1
I think you want
mybounds._southWest.lat
ormybounds['_southWest']['lat']
– Phil Commented Jun 11, 2015 at 1:06 - How did you parse it? – The_Black_Smurf Commented Jun 11, 2015 at 1:06
2 Answers
Reset to default 5JSON.stringify
converts your JavaScript object to string so you can't access it as object like you tried. Stringified JSON is not an object you can access its property. The object should remain object, not stringified. You don't need it in your purpose (if I understand your problem correctly).
Given:
var mybound = {_southWest: e.LatLng, _northEast: e.LatLng}
_northEast: e.LatLng
lat: 50.62507306341435
lng: -69.169921875
__proto__: Object
_southWest: e.LatLng
lat: 27.059125784374068
lng: -127.96875
__proto__: Object
__proto__: Object
To access its property, you can do via:
var lat = mybound["_southWest"]["lat"];
If mybounds is your object, you can just do
mybounds._southWest.lat;
Example:
var str = '{"_southWest":{"lat":27.059125784374068,"lng":-127.96875},"_northEast":{"lat":50.62507306341435,"lng":-69.169921875}}';
var mybounds= JSON.parse(str);
console.log(mybounds._southWest.lat);
27.059125784374068