Reputation: 66
From this data
[{"lat":"-1.325416","lng":"36.669051"},
{"lat":"-1.392932","lng":"36.768752"},
{"lat":"-1.390505","lng":"36.810023"},
{"lat":"-1.448266","lng":"36.952769"},
{"lat":"-1.267033","lng":"37.094882"},
{"lat":"-1.214605","lng":"37.053978"},
{"lat":"-1.169516","lng":"36.895608"}]
I am trying to create a javascript object that looks like this.
var outerCoords =[
{lat: -1.325416, lng: 36.669051},
{lat: -1.392932, lng: 36.768752},
{lat: -1.390505, lng: 36.810023},
{lat: -1.448266, lng: 36.952769},
{lat: -1.267033, lng: 37.094882},
{lat: -1.214605, lng: 37.053978},
{lat: -1.169516, lng: 36.895608},
{lat: -1.244058, lng: 36.730391}
],
the property value without double quotes. I have first, stringified my json to get a string, then removed the double quotes from the string, then parsed the result with no double quotes. Parsing the result does not create an object, it returns a string. Please if you can help i'll appreciate. This is what am doing.
var str= JSON.stringify(outercords1);
var x = str.replace (/"/g,'');
var obj= JSON.parse(x);
the value of outercords is:
[{"lat":"-1.325416","lng":"36.669051"},{"lat":"-1.392932","lng":"36.768752"},{"lat":"-1.390505","lng":"36.810023"},{"lat":"-1.448266","lng":"36.952769"},{"lat":"-1.267033","lng":"37.094882"},{"lat":"-1.214605","lng":"37.053978"},{"lat":"-1.169516","lng":"36.895608"}]
Upvotes: 3
Views: 4083
Reputation: 3752
You can use JSON.parse with a custom reviver
function to manipulate your elements.
var outderCoords1=[{"lat":"-1.325416","lng":"36.669051"},
{"lat":"-1.392932","lng":"36.768752"}];
var outerCoords = JSON.parse(JSON.stringify(outderCoords1),
function(name, value) {
if(!(typeof value.replace === "undefined")){
var v= parseFloat(value.replace(/"/g, ""));
alert(typeof(v));
}
});
Upvotes: 0
Reputation: 138
Well I think don't use stringify
that solve your problem try it as it is.
Upvotes: 1
Reputation: 386560
You could iterating the array and the objects and convert all stringed numbers to number.
The keys have, according to the JSON, always double quotes.
var outerCoords = [{ "lat": "-1.325416", "lng": "36.669051" }, { "lat": "-1.392932", "lng": "36.768752" }, { "lat": "-1.390505", "lng": "36.810023" }, { "lat": "-1.448266", "lng": "36.952769" }, { "lat": "-1.267033", "lng": "37.094882" }, { "lat": "-1.214605", "lng": "37.053978" }, { "lat": "-1.169516", "lng": "36.895608" }];
outerCoords.forEach(function (o) {
Object.keys(o).forEach(function (k) {
if (isFinite(o[k])) {
o[k] = +o[k];
}
});
});
console.log(outerCoords);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 0
Reputation: 356
var outerCoords =[
{lat: -1.325416, lng: 36.669051},
{lat: -1.392932, lng: 36.768752},
{lat: -1.390505, lng: 36.810023},
{lat: -1.448266, lng: 36.952769},
{lat: -1.267033, lng: 37.094882},
{lat: -1.214605, lng: 37.053978},
{lat: -1.169516, lng: 36.895608},
{lat: -1.244058, lng: 36.730391}
]
var str= JSON.stringify(outerCoords);
//var x = str.replace (/"/g,'');
var obj= JSON.parse(str);
console.log(obj);
Upvotes: 0
Reputation: 17481
Stringified JSON has quoted properties. This is mandatory. So a javascript object in the form
var outerCoords =[
{lat: -1.325416, lng: 36.669051},
{lat: -1.392932, lng: 36.768752},
{lat: -1.390505, lng: 36.810023},
{lat: -1.448266, lng: 36.952769},
{lat: -1.267033, lng: 37.094882},
{lat: -1.214605, lng: 37.053978},
{lat: -1.169516, lng: 36.895608},
{lat: -1.244058, lng: 36.730391}
],
Will be stringified as
'[{"lat":-1.325416,"lng":36.669051},{"lat":-1.392932,"lng":36.768752},{"lat":-1.390505,"lng":36.810023},{"lat":-1.448266,"lng":36.952769},{"lat":-1.267033,"lng":37.094882},{"lat":-1.214605,"lng":37.053978},{"lat":-1.169516,"lng":36.895608},{"lat":-1.244058,"lng":36.730391}]'
removing the quotes will turn it invalid to be parsed as JSON, so that's why you're getting a string.
On the other hand, performing
JSON.parse('[{"lat":-1.325416,"lng":36.669051},{"lat":-1.392932,"lng":36.768752},{"lat":-1.390505,"lng":36.810023},{"lat":-1.448266,"lng":36.952769},{"lat":-1.267033,"lng":37.094882},{"lat":-1.214605,"lng":37.053978},{"lat":-1.169516,"lng":36.895608},{"lat":-1.244058,"lng":36.730391}]');
Will give you an object. The same object you hand when you begun.
Object properties are always casted as strings, so declaring your object as
var outerCoords =[
{"lat": -1.325416, "lng": 36.669051},
{"lat": -1.392932, "lng": 36.768752},
{"lat": -1.390505, "lng": 36.810023},
{"lat": -1.448266, "lng": 36.952769},
{"lat": -1.267033, "lng": 37.094882},
{"lat": -1.214605, "lng": 37.053978},
{"lat": -1.169516, "lng": 36.895608},
{"lat": -1.244058, "lng": 36.730391}
];
is the same as declaring it without quotes.
Upvotes: 2