texas697 texas697 - 6 months ago 11
Javascript Question

how to format array of arrays using lodash

I am using lodash to group a array by a property "isRouteId" which looks like this:

[
[
{
"key": 0,
"isSurveyId": null,
"isPausedId": null,
"isRouteId": "7778a8b2-2a92-49f3-b910-089231678412",
"isPaused": false,
"subdivisionId": 0,
"latlngObj": {
"latitude": 29.5407882,
"longitude": -95.7732222
}
},
{
"key": 1,
"isSurveyId": null,
"isPausedId": null,
"isRouteId": "7778a8b2-2a92-49f3-b910-089231678412",
"isPaused": false,
"subdivisionId": 0,
"latlngObj": {
"latitude": 29.5406792,
"longitude": -95.7732779
}
}
],
[
{
"key": 71,
"isSurveyId": null,
"isPausedId": null,
"isRouteId": "ddb42ef3-6c0e-48fc-9bcb-0a64ad422db4",
"isPaused": false,
"subdivisionId": 0,
"latlngObj": {
"latitude": 29.5332458,
"longitude": -95.7766514
}
},
{
"key": 72,
"isSurveyId": null,
"isPausedId": null,
"isRouteId": "ddb42ef3-6c0e-48fc-9bcb-0a64ad422db4",
"isPaused": false,
"subdivisionId": 0,
"latlngObj": {
"latitude": 29.5331976,
"longitude": -95.7765264
}
},
{
"key": 73,
"isSurveyId": null,
"isPausedId": null,
"isRouteId": "ddb42ef3-6c0e-48fc-9bcb-0a64ad422db4",
"isPaused": false,
"subdivisionId": 0,
"latlngObj": {
"latitude": 29.5331486,
"longitude": -95.7763998
}
}
],
[
{
"key": 93,
"isSurveyId": null,
"isPausedId": null,
"isRouteId": "7a343973-95b4-4508-9076-15a6ebae5555",
"isPaused": false,
"subdivisionId": 0,
"latlngObj": {
"latitude": 29.5321796,
"longitude": -95.7738858
}
},
{
"key": 94,
"isSurveyId": null,
"isPausedId": null,
"isRouteId": "7a343973-95b4-4508-9076-15a6ebae5555",
"isPaused": false,
"subdivisionId": 0,
"latlngObj": {
"latitude": 29.5321292,
"longitude": -95.7737588
}
}
]
]


I need help with the next step which is to take the nested "latlngObj" properties and move them to the root level.
Which the Desired Result needs to look like this:

[
[
{
"key": 0,
"isSurveyId": null,
"isPausedId": null,
"isRouteId": "7778a8b2-2a92-49f3-b910-089231678412",
"isPaused": false,
"subdivisionId": 0,
"latitude": 29.5407882,
"longitude": -95.7732222
},
{
"key": 1,
"isSurveyId": null,
"isPausedId": null,
"isRouteId": "7778a8b2-2a92-49f3-b910-089231678412",
"isPaused": false,
"subdivisionId": 0,
"latitude": 29.5406792,
"longitude": -95.7732779
}
],
[
{
"key": 71,
"isSurveyId": null,
"isPausedId": null,
"isRouteId": "ddb42ef3-6c0e-48fc-9bcb-0a64ad422db4",
"isPaused": false,
"subdivisionId": 0,
"latitude": 29.5332458,
"longitude": -95.7766514
},
{
"key": 72,
"isSurveyId": null,
"isPausedId": null,
"isRouteId": "ddb42ef3-6c0e-48fc-9bcb-0a64ad422db4",
"isPaused": false,
"subdivisionId": 0,
"latitude": 29.5331976,
"longitude": -95.7765264
},
{
"key": 73,
"isSurveyId": null,
"isPausedId": null,
"isRouteId": "ddb42ef3-6c0e-48fc-9bcb-0a64ad422db4",
"isPaused": false,
"subdivisionId": 0,
"latitude": 29.5331486,
"longitude": -95.7763998
}
],
[
{
"key": 93,
"isSurveyId": null,
"isPausedId": null,
"isRouteId": "7a343973-95b4-4508-9076-15a6ebae5555",
"isPaused": false,
"subdivisionId": 0,
"latitude": 29.5321796,
"longitude": -95.7738858
},
{
"key": 94,
"isSurveyId": null,
"isPausedId": null,
"isRouteId": "7a343973-95b4-4508-9076-15a6ebae5555",
"isPaused": false,
"subdivisionId": 0,
"latitude": 29.5321292,
"longitude": -95.7737588
}
]
]


I know how to do it using jquery but that is not available. I need to use lodash, underscore or javascript to achieve this.

plunker

var result = _(routesById).groupBy('isRouteId').values().value();

console.log(result);

Answer
 _.each(result,(model) => {
   _.each(model,(_model) => {
     _model.latitude = _model.latlngObj.latitude;
     _model.longitude = _model.latlngObj.longitude;
     delete _model.latlngObj;
   });
});

https://plnkr.co/edit/D1hnaRGZNY8TC1pAARJj?p=preview

Comments