我有可用的lodash 4.17
我有以下API响应结构:
{
"things": [
{
"id": 14500,
"name": "Q1 Out Ind",
"thing_type_id": 6,
"owner_id": 1680,
"owner": {
"id": 1680,
"model_id": 602
},
"thing_type": {
"id": 6,
"name": "The Type of Thing"
},
"related_things": [
{
"id": 9749,
"name": "unnamed thing",
"thing_id": 14500,
"more_things": [
{
"id": 16166,
"name": "Num Thing Sum",
"datasource_object_id": 9408,
"thing_id": 9749,
"external_datasource": {
"id": 9408,
"parent_id": 2810,
"target_id": 15028
}
}
]
}
]
},
{
"id": 14503,
"name": "Q2 Out Ind",
"thing_type_id": 6,
"owner_id": 1681,
"owner": {
"id": 1681,
"model_id": 602
},
"thing_type": {
"id": 6,
"name": "The Type of Thing"
},
"related_things": [
{
"id": 9750,
"name": "unnamed thing2",
"thing_id": 14503,
"more_things": [
{
"id": 16167,
"name": "Num Thing Avg",
"datasource_object_id": 9409,
"thing_id": 9750,
"external_datasource": {
"id": 9409,
"parent_id": 2810,
"target_id": 15029
}
},
{
"id": 16168,
"name": "Num Thing Count",
"datasource_object_id": 9408,
"thing_id": 9750,
"external_datasource": {
"id": 9408,
"parent_id": 2810,
"target_id": 15028
}
}
]
}
]
}
]
}
我正在尝试获取匹配特定target_id
的对象列表,这些对象位于嵌套结构的最末端。
到目前为止,以下代码仅在数组中有一个结果时有效:
_.filter(things.things,
function (obj) {
return obj.related_things[0].more_things[0].external_datasource.target_id == 15028;
}
)
然而,正如您在此示例中所看到的,在这种情况下,它在末尾有两个“things”,因为
more_things
和related_things
都有一个数组,所以存在匹配 - 我该如何调整我的lodash过滤器以跨任何深度搜索?我需要一个匹配对象列表,以显示与匹配目标相关的UI属性。
some
。顺便问一下,你是需要整个大的东西返回还是只需要带有external_datasource
字段的小对象? - cmgchesssome
。 - Carmageddontarget_id
:) - Carmageddon