0

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);

3 Answers 3

3

I would take the following approach:

_(coll)
  .flatten()
  .map(i => _.assign({}, _.omit(i, 'latlngObj'), i.latlngObj))
  .groupBy('isRouteId')
  .value()

The first step is to flatten() the array, so then you can just map() the items. The idea is that the map iteratee uses assign() to add the latlngObj properties, while omit() is used to get rid of latlngObj since it is no longer needed (you can skip the omit() part if you want).

Then it's a simple call to groupBy() to finish off the chain.

Sign up to request clarification or add additional context in comments.

Comments

1
 _.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

1

Here's a solution that only requires one iteration over your collection. We do all the work inside the groupBy iterating function before returning the groupBy key.

var result = _(routesById).groupBy(function(model) {
  model.latitude = model.latlngObj.latitude;
  model.longitude = model.latlngObj.longitude;
  delete(model.latlngObj);
  return model.isRouteId;
}).values().value();

Plunker

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.