Json Result - how to remove quotes
Here is my code
Swipe data = Array[2] :
0:object
id : "1" latitude : "76.23" longitude:"21.92"
1:object
id:"2" latitude:"10.23" longitude:"12.92"
var abc=[];
for(_i = 0; _i < swipe_data.length; _i++)
{
var tmp = swipe_data[_i];
var t1 = [swipe_data[_i]['latitude'], swipe_data[_i]['longitude']];
abc[tmp.id] = JSON.stringify(t1);
}
console.log(abc);
The abc output is
[1: "["76.2350","21.9253"]", 3: "["10.5650","21.3653"]", 6: "["60.5650","55.3653"]"]
this is a json string
but I need output like this: {"1": [76.235,21.9253], "3": [10.565,21.3653], "6": [60.565.55.3653]}
Any solution for this.?
+3
source to share
2 answers
Use parseFloat
,
var abc = new Object();
for(_i = 0; _i < swipe_data.length; _i++){
var t1 = [parseFloat(swipe_data[_i]['latitude']), parseFloat(swipe_data[_i]['longitude'])];
abc[tmp.id] = t1
}
console.log(JSON.stringify(abc));
Try to run this code, it should work.
Note:
-
abc
shouldObject
not be aArray
. - Not
JSON.stringify
while asscigning meaningkey
. - Execute
JSON.stringify
after updating all key values inObject
.
0
source to share
A well obvious solution is this:
var abc=[];
for(_i = 0; _i < swipe_data.length; _i++)
{
var tmp = swipe_data[_i];
var t1 = [swipe_data[_i]['latitude'], swipe_data[_i]['longitude']];
abc[tmp.id] = JSON.stringify(t1);
abc[tmp.id] = abc[tmp.id].replace('"','');
}
console.log(abc);
All it does is replace any quotes in the string with nothing, effectively removing them.
0
source to share