我有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;
}
)
然而,正如你在这个例子中看到的,在这个例子中,它有两个"东西"在其中结束时,有一个匹配,因为有数组的more_things
和related_things
-我如何调整我的lodash过滤器在任何深度搜索?我需要一个匹配对象的列表,以便在UI中显示与匹配目标相关的各种属性。
您可以在filter
中嵌套_some
函数。顺便说一下,本机数组filter
和some
也可以这样做。
const things = { "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, "form_id": 2810, "target_id": 15028 } } ] } ] } ]}
const id = 15028
const res = _.filter(things.things, a => {
return _.some(a.related_things, b => {
return _.some(b.more_things, c => c.external_datasource.target_id === id)
})
})
console.log(res)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
没有lodash
const things = { "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, "form_id": 2810, "target_id": 15028 } } ] } ] } ]}
const id = 15028
const res = things.things.filter(a => {
return a.related_things.some(b => {
return b.more_things.some(c => c.external_datasource.target_id === id)
})
})
console.log(res)
您可以将flatMap
和some
与filter
一起使用
const things= {
"things": [
{
"id": 1450033333,
"name": "SHOULD NOT BE IN THE RESULTS",
"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": 15024
}
}
]
}
]
},
{
"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
}
}
]
}
]
}
]
}
const results = _.filter(things.things, thing => {
const relatedThings = _.flatMap(thing.related_things, 'more_things');
return _.some(relatedThings, moreThing => moreThing.external_datasource.target_id == 15028);
});
console.log(results)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>