如何反转嵌套数组/对象结构并重新以最低对象重新键入?

huangapple go评论51阅读模式
英文:

How to Invert nested array/objects structure and rekeying by lowest object?

问题

I am your Chinese translator, translating only the specified content without any additional information. Here is the translated content:

我有 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 中的各种属性。
英文:

I have lodash 4.17 available

I have the following API response structure:

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

And I am trying to get a list of objects matching specific target_id at the very end of the nesting.

So far, the following works only if there is one result in the array:

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

However, as you see in this example, in this case it has two "things" at the end of which, there is a match as there is array for more_things and related_things - how do I adjust my lodash filter to search across any depth? I need a list of matching objects, to display various attributes in the UI related to the matched targets.

答案1

得分: 1

你可以在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 = 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);
英文:

you can have nested _some functions inside the filter. By the way this is also possible with native array filter and some

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const things = {    &quot;things&quot;: [        {            &quot;id&quot;: 14500,            &quot;name&quot;: &quot;Q1 Out Ind&quot;,            &quot;thing_type_id&quot;: 6,            &quot;owner_id&quot;: 1680,            &quot;owner&quot;: {                &quot;id&quot;: 1680,                &quot;model_id&quot;: 602            },            &quot;thing_type&quot;: {                &quot;id&quot;: 6,                &quot;name&quot;: &quot;The Type of Thing&quot;            },            &quot;related_things&quot;: [                {                    &quot;id&quot;: 9749,                    &quot;name&quot;: &quot;unnamed thing&quot;,                    &quot;thing_id&quot;: 14500,                    &quot;more_things&quot;: [                        {                            &quot;id&quot;: 16166,                            &quot;name&quot;: &quot;Num Thing Sum&quot;,                            &quot;datasource_object_id&quot;: 9408,                            &quot;thing_id&quot;: 9749,                            &quot;external_datasource&quot;: {                                &quot;id&quot;: 9408,                                &quot;parent_id&quot;: 2810,                                &quot;target_id&quot;: 15028                            }                        }                    ]                }            ]        },        {            &quot;id&quot;: 14503,            &quot;name&quot;: &quot;Q2 Out Ind&quot;,            &quot;thing_type_id&quot;: 6,            &quot;owner_id&quot;: 1681,            &quot;owner&quot;: {                &quot;id&quot;: 1681,                &quot;model_id&quot;: 602            },            &quot;thing_type&quot;: {                &quot;id&quot;: 6,                &quot;name&quot;: &quot;The Type of Thing&quot;            },            &quot;related_things&quot;: [                {                    &quot;id&quot;: 9750,                    &quot;name&quot;: &quot;unnamed thing2&quot;,                    &quot;thing_id&quot;: 14503,                    &quot;more_things&quot;: [                        {                            &quot;id&quot;: 16167,                            &quot;name&quot;: &quot;Num Thing Avg&quot;,                            &quot;datasource_object_id&quot;: 9409,                            &quot;thing_id&quot;: 9750,                            &quot;external_datasource&quot;: {                                &quot;id&quot;: 9409,                                &quot;parent_id&quot;: 2810,                                &quot;target_id&quot;: 15029                            }                        },                        {                            &quot;id&quot;: 16168,                            &quot;name&quot;: &quot;Num Thing Count&quot;,                            &quot;datasource_object_id&quot;: 9408,                            &quot;thing_id&quot;: 9750,                            &quot;external_datasource&quot;: {                                &quot;id&quot;: 9408,                                &quot;form_id&quot;: 2810,                                &quot;target_id&quot;: 15028                            }                        }                    ]                }            ]        }    ]}
const id = 15028
const res = _.filter(things.things, a =&gt; {
return _.some(a.related_things, b =&gt; {
return _.some(b.more_things, c =&gt; c.external_datasource.target_id === id)
})
})
console.log(res)

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

without lodash

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const things = {    &quot;things&quot;: [        {            &quot;id&quot;: 14500,            &quot;name&quot;: &quot;Q1 Out Ind&quot;,            &quot;thing_type_id&quot;: 6,            &quot;owner_id&quot;: 1680,            &quot;owner&quot;: {                &quot;id&quot;: 1680,                &quot;model_id&quot;: 602            },            &quot;thing_type&quot;: {                &quot;id&quot;: 6,                &quot;name&quot;: &quot;The Type of Thing&quot;            },            &quot;related_things&quot;: [                {                    &quot;id&quot;: 9749,                    &quot;name&quot;: &quot;unnamed thing&quot;,                    &quot;thing_id&quot;: 14500,                    &quot;more_things&quot;: [                        {                            &quot;id&quot;: 16166,                            &quot;name&quot;: &quot;Num Thing Sum&quot;,                            &quot;datasource_object_id&quot;: 9408,                            &quot;thing_id&quot;: 9749,                            &quot;external_datasource&quot;: {                                &quot;id&quot;: 9408,                                &quot;parent_id&quot;: 2810,                                &quot;target_id&quot;: 15028                            }                        }                    ]                }            ]        },        {            &quot;id&quot;: 14503,            &quot;name&quot;: &quot;Q2 Out Ind&quot;,            &quot;thing_type_id&quot;: 6,            &quot;owner_id&quot;: 1681,            &quot;owner&quot;: {                &quot;id&quot;: 1681,                &quot;model_id&quot;: 602            },            &quot;thing_type&quot;: {                &quot;id&quot;: 6,                &quot;name&quot;: &quot;The Type of Thing&quot;            },            &quot;related_things&quot;: [                {                    &quot;id&quot;: 9750,                    &quot;name&quot;: &quot;unnamed thing2&quot;,                    &quot;thing_id&quot;: 14503,                    &quot;more_things&quot;: [                        {                            &quot;id&quot;: 16167,                            &quot;name&quot;: &quot;Num Thing Avg&quot;,                            &quot;datasource_object_id&quot;: 9409,                            &quot;thing_id&quot;: 9750,                            &quot;external_datasource&quot;: {                                &quot;id&quot;: 9409,                                &quot;parent_id&quot;: 2810,                                &quot;target_id&quot;: 15029                            }                        },                        {                            &quot;id&quot;: 16168,                            &quot;name&quot;: &quot;Num Thing Count&quot;,                            &quot;datasource_object_id&quot;: 9408,                            &quot;thing_id&quot;: 9750,                            &quot;external_datasource&quot;: {                                &quot;id&quot;: 9408,                                &quot;form_id&quot;: 2810,                                &quot;target_id&quot;: 15028                            }                        }                    ]                }            ]        }    ]}
const id = 15028
const res = things.things.filter(a =&gt; {
return a.related_things.some(b =&gt; {
return b.more_things.some(c =&gt; c.external_datasource.target_id === id)
})
})
console.log(res)

<!-- end snippet -->

答案2

得分: 0

你可以使用flatMapsomefilter

英文:

You can use flatMap and some with filter

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const things= {
&quot;things&quot;: [

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

        {
&quot;id&quot;: 14500,
&quot;name&quot;: &quot;Q1 Out Ind&quot;,
&quot;thing_type_id&quot;: 6,
&quot;owner_id&quot;: 1680,
&quot;owner&quot;: {
&quot;id&quot;: 1680,
&quot;model_id&quot;: 602
},
&quot;thing_type&quot;: {
&quot;id&quot;: 6,
&quot;name&quot;: &quot;The Type of Thing&quot;
},
&quot;related_things&quot;: [
{
&quot;id&quot;: 9749,
&quot;name&quot;: &quot;unnamed thing&quot;,
&quot;thing_id&quot;: 14500,
&quot;more_things&quot;: [
{
&quot;id&quot;: 16166,
&quot;name&quot;: &quot;Num Thing Sum&quot;,
&quot;datasource_object_id&quot;: 9408,
&quot;thing_id&quot;: 9749,
&quot;external_datasource&quot;: {
&quot;id&quot;: 9408,
&quot;parent_id&quot;: 2810,
&quot;target_id&quot;: 15028
}
}
]
}
]
},
{
&quot;id&quot;: 14503,
&quot;name&quot;: &quot;Q2 Out Ind&quot;,
&quot;thing_type_id&quot;: 6,
&quot;owner_id&quot;: 1681,
&quot;owner&quot;: {
&quot;id&quot;: 1681,
&quot;model_id&quot;: 602
},
&quot;thing_type&quot;: {
&quot;id&quot;: 6,
&quot;name&quot;: &quot;The Type of Thing&quot;
},
&quot;related_things&quot;: [
{
&quot;id&quot;: 9750,
&quot;name&quot;: &quot;unnamed thing2&quot;,
&quot;thing_id&quot;: 14503,
&quot;more_things&quot;: [
{
&quot;id&quot;: 16167,
&quot;name&quot;: &quot;Num Thing Avg&quot;,
&quot;datasource_object_id&quot;: 9409,
&quot;thing_id&quot;: 9750,
&quot;external_datasource&quot;: {
&quot;id&quot;: 9409,
&quot;parent_id&quot;: 2810,
&quot;target_id&quot;: 15029
}
},
{
&quot;id&quot;: 16168,
&quot;name&quot;: &quot;Num Thing Count&quot;,
&quot;datasource_object_id&quot;: 9408,
&quot;thing_id&quot;: 9750,
&quot;external_datasource&quot;: {
&quot;id&quot;: 9408,
&quot;parent_id&quot;: 2810,
&quot;target_id&quot;: 15028
}
}
]
}
]
}
]
}
const results = _.filter(things.things, thing =&gt; {
const relatedThings = _.flatMap(thing.related_things, &#39;more_things&#39;);
return _.some(relatedThings, moreThing =&gt; moreThing.external_datasource.target_id == 15028);
});
console.log(results)

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月11日 01:12:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定