我有Lotash 4.17的空位

我的接口响应 struct 如下:

{
    "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
                            }
                        }
                    ]
                }
            ]
        }
    ]
}

我正在try 获取与嵌套末尾的特定target_id相匹配的对象列表.

到目前为止,以下操作仅在数组中有一个结果时才起作用:

_.filter(things.things, 
      function (obj) {
        return obj.related_things[0].more_things[0].external_datasource.target_id == 15028;
      }
  )

然而,正如您在本例中看到的,在本例中,它有两个"东西",在最后有一个匹配,因为有more_thingsrelated_things的数组--我如何调整我的寄生过滤器来搜索任何深度?我需要匹配对象的列表,以在UI中显示与匹配目标相关的各种属性.

推荐答案

您可以在filter中嵌套_some个函数.顺便说一句,本机数组filtersome也可以做到这一点

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>

不含罗达什

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)

Javascript相关问答推荐

使用NgDeliverentOutlet和动态内容投影的Angular 渲染组件

如何使用React渲染器放置根dis?

React Hooks中useState的同步问题

如何通过onClick为一组按钮分配功能;

Google Apps脚本中的discord邀请API响应的日期解析问题

如何使覆盖div与可水平滚动的父div相关?

在react js中使用react—router—dom中的Link组件,分配的右侧不能被 destruct ''

将旋转的矩形zoom 到覆盖它所需的最小尺寸&S容器

无法读取未定义错误的属性路径名''

如何使用子字符串在数组中搜索重复项

S文本内容和值不必要的不同

提交链接到AJAX数据结果的表单

使用auth.js保护API路由的Next.JS,FETCH()不起作用

使用插件构建包含chart.js提供程序的Angular 库?

将相关数据组合到两个不同的数组中

react 路由如何使用从加载器返回的数据

P5.js中矩形内的圆弧

我的NavLink活动类在REACT-ROUTER-V6中出现问题

重新渲染过多(&Q).REACT限制渲染次数以防止无限循环.使用REACT下拉菜单时

在范围数组中查找公共(包含)范围