我使用lodash按属性“isRouteId”对数组进行分组,如下所示:
[
[
{
"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
}
}
]
]
我需要帮助下一步,即采用嵌套的“latlngObj”属性并将它们移动到根级别。 所需结果需要如下所示:
[
[
{
"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
}
]
]
我知道如何使用jquery做到这一点,但这是不可用的。我需要使用lodash,下划线或javascript来实现这一目标。
var result = _(routesById).groupBy('isRouteId').values().value();
console.log(result);
答案 0 :(得分:3)
我会采取以下方法:
_(coll)
.flatten()
.map(i => _.assign({}, _.omit(i, 'latlngObj'), i.latlngObj))
.groupBy('isRouteId')
.value()
第一步是flatten()数组,这样你就可以map()项目了。我们的想法是,地图iteratee使用assign()添加latlngObj
属性,而omit()用于删除latlngObj
,因为不再需要它(您可以跳过如果你想要omit()
部分。)
然后,只需简单调用groupBy()即可完成链条。
答案 1 :(得分:1)
_.each(result,(model) => {
_.each(model,(_model) => {
_model.latitude = _model.latlngObj.latitude;
_model.longitude = _model.latlngObj.longitude;
delete _model.latlngObj;
});
});
答案 2 :(得分:1)
这是一个只需要对您的收藏进行一次迭代的解决方案。在返回groupBy键之前,我们在groupBy
迭代函数中完成所有工作。
var result = _(routesById).groupBy(function(model) {
model.latitude = model.latlngObj.latitude;
model.longitude = model.latlngObj.longitude;
delete(model.latlngObj);
return model.isRouteId;
}).values().value();