I have a JSON in the below format
waypoints = [
{
lat: 22,
lng: 44
},
{
lat: 55,
lng: 77
}
]
I need to convert it into the following format using JS
[[22, 44], [55, 77]]
Please can some one help me with a solution
I have a JSON in the below format
waypoints = [
{
lat: 22,
lng: 44
},
{
lat: 55,
lng: 77
}
]
I need to convert it into the following format using JS
[[22, 44], [55, 77]]
Please can some one help me with a solution
You can create a new array with use of .map method which in execution creates a new array without modifying the original array:
var waypoints = [{
lat: 22,
lng: 44
}, {
lat: 55,
lng: 77
}];
var newArr = waypoints.map(function(obj) {
return [obj.lat, obj.lng];
});
document.body.textContent = JSON.stringify(newArr);
var waypoints = [
{
lat: 22,
lng: 44
},
{
lat: 55,
lng: 77
}
];
var array = [];
for (i in waypoints) {
array.push([]);
for (j in waypoints[i]) {
array[i].push(waypoints[i][j]);
}
}