Related
I have an array of objects which I need to restructure. The input array of objects is as follows:
[
[
{
"lat": 51.00445965809624,
"lng": 5.505536355741434
},
{
"lat": 51.00431971638806,
"lng": 5.505461423372382
},
{
"lat": 51.00417977463196,
"lng": 5.505386491455417
},
{
"lat": 51.00403983282792,
"lng": 5.505311559990534
},
{
"lat": 51.00389989097598,
"lng": 5.505236628977724
},
{
"lat": 51.00375994907609,
"lng": 5.505161698416986
},
{
"lat": 51.00362000712828,
"lng": 5.505086768308312
}
],
[
{
"lat": 51.00440846173839,
"lng": 5.505785160705168
},
{
"lat": 51.00426821144456,
"lng": 5.505709911534925
},
{
"lat": 51.00412796110243,
"lng": 5.50563466281968
},
{
"lat": 51.00398771071194,
"lng": 5.5055594145594275
},
{
"lat": 51.00384746027312,
"lng": 5.505484166754162
},
{
"lat": 51.00370720978598,
"lng": 5.5054089194038776
},
{
"lat": 51.0035669592505,
"lng": 5.50533367250857
}
],
[
{
"lat": 51.004357264852146,
"lng": 5.506033965119733
},
{
"lat": 51.00421670597403,
"lng": 5.505958399145694
},
{
"lat": 51.004076147047186,
"lng": 5.505882833629572
},
{
"lat": 51.003935588071585,
"lng": 5.505807268571363
},
{
"lat": 51.00379502904726,
"lng": 5.505731703971063
},
{
"lat": 51.003654469974194,
"lng": 5.5056561398286625
},
{
"lat": 51.00351391085237,
"lng": 5.505580576144162
}
],
[
{
"lat": 51.00430606743754,
"lng": 5.506282768985127
},
{
"lat": 51.00416519997647,
"lng": 5.506206886204688
},
{
"lat": 51.00402433246626,
"lng": 5.506131003885096
},
{
"lat": 51.00388346490688,
"lng": 5.506055122026344
},
{
"lat": 51.00374259729837,
"lng": 5.505979240628427
},
{
"lat": 51.00360172964068,
"lng": 5.505903359691342
},
{
"lat": 51.003460861933874,
"lng": 5.505827479215082
}
]
]
In actuality these are the coordinates of pitch blocks here as below:
If you see there are four row lines ( each block has two up and down ) and seven column lines ( each block has two, left and right line). so array has four main array and seven objects under that.
Can I get the structure of array so that it should be like the coordinates of each block in array? e.g.
[
[
[
{ lat: 51.00445965809624, lng: 5.505536355741434 },
{ lat: 51.00431971638806, lng: 5.505461423372382 },
{ lat: 51.00426821144456, lng: 5.505709911534925 },
{ lat: 51.00440846173839, lng: 5.505785160705168 },
{ lat: 51.00445965809624, lng: 5.505536355741434 }
],[
....
]
] ,[
....
]
]
Below array structure, if for bottom left block number 54.
[
[
[
{ lat: 51.00445965809624, lng: 5.505536355741434 },
{ lat: 51.00431971638806, lng: 5.505461423372382 },
{ lat: 51.00426821144456, lng: 5.505709911534925 },
{ lat: 51.00440846173839, lng: 5.505785160705168 },
{ lat: 51.00445965809624, lng: 5.505536355741434 }
],[
....
]
] ,[
{},{}..
]
]
You'll have to iterate the original array and make the "box" combinations. There are many ways to do that. Here is how you can use flatMap and map to perform the nested iteration along row/column dimensions:
const points = [[{"lat": 51.00445965809624,"lng": 5.505536355741434},{"lat": 51.00431971638806,"lng": 5.505461423372382},{"lat": 51.00417977463196,"lng": 5.505386491455417},{"lat": 51.00403983282792,"lng": 5.505311559990534},{"lat": 51.00389989097598,"lng": 5.505236628977724},{"lat": 51.00375994907609,"lng": 5.505161698416986},{"lat": 51.00362000712828,"lng": 5.505086768308312}],[{"lat": 51.00440846173839,"lng": 5.505785160705168},{"lat": 51.00426821144456,"lng": 5.505709911534925},{"lat": 51.00412796110243,"lng": 5.50563466281968},{"lat": 51.00398771071194,"lng": 5.5055594145594275},{"lat": 51.00384746027312,"lng": 5.505484166754162},{"lat": 51.00370720978598,"lng": 5.5054089194038776},{"lat": 51.0035669592505,"lng": 5.50533367250857}],[{"lat": 51.004357264852146,"lng": 5.506033965119733},{"lat": 51.00421670597403,"lng": 5.505958399145694},{"lat": 51.004076147047186,"lng": 5.505882833629572}, {"lat": 51.003935588071585,"lng": 5.505807268571363},{"lat": 51.00379502904726,"lng": 5.505731703971063},{"lat": 51.003654469974194,"lng": 5.5056561398286625},{"lat": 51.00351391085237,"lng": 5.505580576144162}],[{"lat": 51.00430606743754,"lng": 5.506282768985127},{"lat": 51.00416519997647,"lng": 5.506206886204688},{"lat": 51.00402433246626,"lng": 5.506131003885096},{"lat": 51.00388346490688,"lng": 5.506055122026344},{"lat": 51.00374259729837,"lng": 5.505979240628427},{"lat": 51.00360172964068,"lng": 5.505903359691342},{"lat": 51.003460861933874,"lng": 5.505827479215082}]];
const boxes = points.slice(1).flatMap((row, y) =>
row.slice(1).map((_, x) => [
...points[y].slice(x, x + 2),
...row.slice(x, x + 2)
])
);
console.log(boxes);
Note that the same object will be used multiple times in the output, as an object represents a point that can be shared by up to four neighboring boxes. In the above stack snippet output, this reuse of the same objects is marked with /*id*/ and /*ref*/ comments.
I have a string that looks like the following:
{
"results": [
{
"address_components": [
{
"long_name": "Ireland",
"short_name": "Ireland",
"types": [
"establishment",
"natural_feature"
]
}
],
"formatted_address": "Ireland",
"geometry": {
"bounds": {
"northeast": {
"lat": 55.38294149999999,
"lng": -5.431909999999999
},
"southwest": {
"lat": 51.4475448,
"lng": -10.4800237
}
}],
"status" : "OK"
}
I want to turn this into a json that I can easily traverse. However when I call JSON.parse() on this string I get a json array that is a pain to traverse. I want to be able to extract the lat and lng from northeast without having to loop through the entire array.
var json = `{
"results": [
{
"address_components": [
{
"long_name": "Ireland",
"short_name": "Ireland",
"types": [
"establishment",
"natural_feature"
]
}
],
"formatted_address": "Ireland",
"geometry": {
"bounds": {
"northeast": {
"lat": 55.38294149999999,
"lng": -5.431909999999999
},
"southwest": {
"lat": 51.4475448,
"lng": -10.4800237
}
}
}
}
],
"status" : "OK"
}`;
var data = JSON.parse(json);
var bounds = data.results[0].geometry.bounds;
var bound = bounds.northeast || bounds.southwest;
console.log(bound.lat, bound.lng);
If you know northeast is always an option you can simplify this to:
console.log(data.results[0].geometry.bounds.northeast);
That will give you your lat and lng.
I am trying banging my head to filter json written below to get desired result.
[{
"Key": "EShSOKthupE=",
"ImageUrl": "path",
"Title": "ABC",
"CityId": 16,
"TimezoneShortName": "PYT",
"UtcHrsDiff": 8.5,
"PlaceKey": "QIZHdWOa77o=",
"PlaceName": "Shymala Hills Slums",
"Lat": 23.2424856,
"Long": 77.39488289999997,
"ActivityList": [ "Test Activity" ]
},
{
"Key": "NXLQpZAZT4M=",
"ImageUrl": "",
"Title": "ASAS",
"CityId": 17,
"TimezoneShortName": "AEST",
"UtcHrsDiff": 10,
"PlaceKey": "o4fAkahBzYY=",
"PlaceName": "ASAS",
"Lat": 12.9856503,
"Long": 77.60569269999996,
"ActivityList": [ "Adventure Sport" ]
}]
Now I want to get json like this from above json using lodash or undescore js.
[{
"PlaceKey": "QIZHdWOa77o=",
"PlaceName": "ABC",
"Lat": 23.2424856,
"Long": 77.39488289999997
},
{
"PlaceKey": "o4fAkahBzYY=",
"PlaceName": "ASAS",
"Lat": 12.9856503,
"Long": 77.60569269999996,
}]
Any help I can get on this?
Using lodash:
_.map(yourArray, (el => _.pick(el, ['PlaceKey', 'PlaceName', 'Lat', 'Long'])))
You can do this simply in Javascript, without the need of any external library like :
filteredArray=arr.map(function(item){
return {
"PlaceKey": item.PlaceKey,
"PlaceName": item.PlaceName,
"Lat": item.Lat,
"Long": item.Long,
}
})
use _.map function
var finalArray = _.map(your_arr, function(obj) {
return {
"PlaceKey": obj.PlaceKey,
"PlaceName": obj.PlaceName,
"Lat": obj.Lat,
"Long": obj.Long,
}
});
or just use the arrays map function:
var finalArray = your_arr.map(function(obj) {
return {
"PlaceKey": obj.PlaceKey,
"PlaceName": obj.PlaceName,
"Lat": obj.Lat,
"Long": obj.Long,
}
});
I'm trying to use GraphHopper Route Optimization API for solving VRP with pickups and deliveries. I want to test it with an example from https://graphhopper.com/dashboard/#/editor. My request goes like this:
var vrp = {
"vehicles": [
{
"vehicle_id": "my_vehicle",
"start_address": {
"location_id": "berlin",
"lon": 13.406,
"lat": 52.537
}
}
],
"services": [
{
"id": "hamburg",
"name": "visit_hamburg",
"address": {
"location_id": "hamburg",
"lon": 9.999,
"lat": 53.552
}
},
{
"id": "munich",
"name": "visit_munich",
"address": {
"location_id": "munich",
"lon": 11.57,
"lat": 48.145
}
},
{
"id": "cologne",
"name": "visit_cologne",
"address": {
"location_id": "cologne",
"lon": 6.957,
"lat": 50.936
}
},
{
"id": "frankfurt",
"name": "visit_frankfurt",
"address": {
"location_id": "frankfurt",
"lon": 8.67,
"lat": 50.109
}
}
]
};
$.ajax({
beforeSend: function(xhrObj){
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Accept","application/json");
},
type: "POST",
url: 'https://graphhopper.com/api/1/vrp/optimize?key=[...]',
data: vrp,
dataType: "json",
success: function(json){
console.log(json);
}});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
I get the following response:
screenshot
Waht may be the problem here?
I'have found solution in another so-question Send JSON data with jQuery.
Just use data: JSON.stringify(vrp)
I'm parsing JSON response from the MapQuest API. I need all of the 'narratives' from the 'legs' node under 'maneuvers' (legs-> maneuvers -> get all narratives), but I cannot figure it out.
I can get most of the values like so:
$(function(){
var mq = jQuery.parseJSON(jsonResponse);
$("#fuel").html(mq.renderMatrixResults[0].route.fuelUsed);
$("#rtime").html(mq.renderMatrixResults[0].route.realTime);
});
Part of JSON response:
{
"renderMatrixResults": [
{
"route": {
"hasTollRoad": true,
"computedWaypoints": [
],
"fuelUsed": 2.24,
"hasUnpaved": false,
"hasHighway": true,
"realTime": 5556,
"boundingBox": {
"ul": {
"lng": -74.240074,
"lat": 40.662548
},
"lr": {
"lng": -74.132072,
"lat": 40.573451
}
},
"distance": 38.998,
"time": 5523,
"hasSeasonalClosure": false,
"locations": [
{
"latLng": {
"lng": -74.18862,
"lat": 40.609712
},
"adminArea4": "Brooklyn",
"adminArea5Type": "City",
"adminArea4Type": "County",
"adminArea5": "Brooklyn",
"street": "1234 Test Lane",
"adminArea1": "US",
"adminArea3": "NY",
"type": "s",
"displayLatLng": {
"lng": -74.188621,
"lat": 40.60971
},
"linkId": 33589148,
"postalCode": "10001-6992",
"sideOfStreet": "L",
"dragPoint": false,
"adminArea1Type": "Country",
"geocodeQuality": "POINT",
"geocodeQualityCode": "P1AAA",
"adminArea3Type": "State"
},
{
"latLng": {
"lng": -74.194858,
"lat": 40.601623
},
"adminArea4": "Brooklyn",
"adminArea5Type": "City",
"adminArea4Type": "County",
"adminArea5": "Brooklyn",
"street": "5678 Example Street",
"adminArea1": "US",
"adminArea3": "NY",
"type": "s",
"displayLatLng": {
"lng": -74.194854,
"lat": 40.601623
},
"linkId": 33361764,
"postalCode": "10001-5483",
"sideOfStreet": "R",
"dragPoint": false,
"adminArea1Type": "Country",
"geocodeQuality": "POINT",
"geocodeQualityCode": "P1AAA",
"adminArea3Type": "State"
}
],
"hasCountryCross": false,
"legs": [
{
"hasTollRoad": false,
"index": 0,
"roadGradeStrategy": [
[
]
],
"hasHighway": false,
"hasUnpaved": false,
"distance": 0.882,
"time": 145,
"origIndex": 1,
"hasSeasonalClosure": false,
"origNarrative": "Go west on some road",
"hasCountryCross": false,
"formattedTime": "00:02:25",
"destNarrative": "Proceed to 789 GIRAFFE STREET",
"destIndex": 1,
"maneuvers": [
{
"signs": [
],
"index": 0,
"maneuverNotes": [
],
"direction": 4,
"narrative": "Start out going south on Elephant Avenue.",
"iconUrl": "https://content.mapquest.com/mqsite/turnsigns/icon-dirs-start_sm.gif",
"distance": 0.57,
"time": 79,
"linkIds": [
],
"streets": [
"Elephant Avenue"
],
"attributes": 0,
"transportMode": "AUTO",
"formattedTime": "00:01:19",
"directionName": "South",
"mapUrl": "https://www.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luur20uanq,b0=o5-9ayxdw&type=map&size=225,160&pois=purple-1,40.60971,-74.188621,0,0|purple-2,40.602351999999996,-74.189582,0,0|�er=40.606031,-74.18910149999999&zoom=10&rand=-1416511403&session=53c1fb45-030d-0004-02b7-16b5-00163e4c0d3f",
"startPoint": {
"lng": -74.188621,
"lat": 40.60971
},
"turnType": 2
},
{
"signs": [
],
"index": 1,
"maneuverNotes": [
],
"direction": 7,
"narrative": "Turn right onto Tiger Blvd.",
"iconUrl": "https://content.mapquest.com/mqsite/turnsigns/rs_right_sm.gif",
"distance": 0.269,
"time": 56,
"linkIds": [
],
"streets": [
"Tiger Blvd"
],
"attributes": 0,
"transportMode": "AUTO",
"formattedTime": "00:00:56",
"directionName": "West",
"mapUrl": "https://www.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luur20uanq,b0=o5-9ayxdw&type=map&size=225,160&pois=purple-2,40.602351999999996,-74.189582,0,0|purple-3,40.601127,-74.194366,0,0|�er=40.601739499999994,-74.191974&zoom=12&rand=-1416511403&session=53c1fb45-030d-0004-02b7-16b5-00163e4c0d3f",
"startPoint": {
"lng": -74.189582,
"lat": 40.602352
},
"turnType": 2
}
Haven't tested it but here is the idea. You can use a function that will traverse object tree and collect items specified by a path.
function getPath(path, obj) {
var name, value;
path = path instanceof Array ? path: path.split('.'); //convert path to array
name = path.shift(); //get the first name
value = obj[name]; //extract value
if(!path.length || value == null) { //last name or value is null or undefined
return value;
}
if(value instanceof Array) { //if value is array - concat
return [].concat.apply([], value.map(function(item){
return getPath(path.slice(0), item);
}));
}
return getPath(path, value); //else go deeper
}
Usage:
var narratives
= getPath('renderMatrixResults.route.legs.maneuvers.narrative', mq);
I believe the path is correct.