如何使用递归循环获取所有的ID?

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

How to use recursive loop to get all the IDs?

问题

let fids = [];

1st way
--------

let folder = detailsArray.map(el => el.files).flat();
fids = folder.map(el => el.fID);


2nd way
---------

detailsArray.forEach(el => {
if (el.files.length > 0) {
fids = fids.concat(el.files.map(f => f.fID));
}
});


<details>
<summary>英文:</summary>

let detailsArray = [
{
"foldername":"myfolder",
"files":[
{
"fID":"55",
"name":"file51",
},
],
"files":[
{
"foldername":"mySubFolder",
"forms":[
{
"fID":"1111",
"name":"file01",
},
],
"folder":[

             ]
          },
          {
            &quot;foldername&quot;:&quot;mySubFolder02&quot;,
            &quot;forms&quot;:[
               {
                  &quot;fID&quot;:&quot;4444&quot;,
                  &quot;name&quot;:&quot;file02&quot;
               },
            ],
            &quot;folder&quot;:[
               
            ]
         },
       ],
    },
    {
        &quot;foldername&quot;:&quot;myfolder2&quot;,
        &quot;files&quot;:[
           
        ],
        &quot;files&quot;:[
           {
              &quot;foldername&quot;:&quot;mySubFolder2&quot;,
              &quot;forms&quot;:[
                 {
                    &quot;fID&quot;:&quot;2222&quot;,
                    &quot;name&quot;:&quot;file03&quot;,
                 },
              ],
              &quot;folder&quot;:[
                 
              ]
           },
           {
             &quot;foldername&quot;:&quot;mySubFolder03&quot;,
             &quot;forms&quot;:[
                {
                   &quot;fID&quot;:&quot;3333&quot;,
                   &quot;name&quot;:&quot;file03&quot;
                },
             ],
             &quot;folder&quot;:[
                
             ]
          },
        ],
     },
]

I wanted to get all the **fID** values which is in files arrays in above given array. Firstly I tried to get all the fIDs like this 

    let fids = [];

1st way
--------

let folder= detailsArray .map(el => el.folder);
fids = folder.map(el => el.fID);

 
2nd way
---------

detailsArray.forEach(el =>{

  if(el.folder.length &gt; 0)
      allFormIds = el.folder.map(f =&gt; f.fID)

})


Still I couldn&#39;t get correct way to do this. I wanted to use recursive loop for this question.

here is the expected output : **fids = [55,1111,4444,2222,3333]**


</details>


# 答案1
**得分**: 1

```js
    let detailsArray = [ { foldername: "myfolder", files: [], files: [ { foldername: "mySubFolder", forms: [ { fID: "1111", name: "file01", }, ], folder: [], }, { foldername: "mySubFolder02", forms: [ { fID: "4444", name: "file02", }, ], folder: [], }, ], }, { foldername: "myfolder2", files: [], files: [ { foldername: "mySubFolder2", forms: [ { fID: "2222", name: "file03", }, ], folder: [], }, { foldername: "mySubFolder03", forms: [ { fID: "3333", name: "file03", }, ], folder: [], }, ], }, ];

    let output = detailsArray
      .flatMap(({ files }) => files)
      .flatMap(({ forms }) => forms.map(({ fID }) => fID))

    console.log(output);
英文:

Try like below:

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

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

let detailsArray = [ { foldername: &quot;myfolder&quot;, files: [], files: [ { foldername: &quot;mySubFolder&quot;, forms: [ { fID: &quot;1111&quot;, name: &quot;file01&quot;, }, ], folder: [], }, { foldername: &quot;mySubFolder02&quot;, forms: [ { fID: &quot;4444&quot;, name: &quot;file02&quot;, }, ], folder: [], }, ], }, { foldername: &quot;myfolder2&quot;, files: [], files: [ { foldername: &quot;mySubFolder2&quot;, forms: [ { fID: &quot;2222&quot;, name: &quot;file03&quot;, }, ], folder: [], }, { foldername: &quot;mySubFolder03&quot;, forms: [ { fID: &quot;3333&quot;, name: &quot;file03&quot;, }, ], folder: [], }, ], }, ];

let output = detailsArray
  .flatMap(({ files }) =&gt; files)
  .flatMap(({ forms }) =&gt; forms.map(({ fID }) =&gt; fID))

console.log(output);

<!-- end snippet -->

Using Array.prototype.flatMap()

答案2

得分: 0

正如您要求的递归解决方案,我认为这个解决方案对您应该有效。
英文:

As you asked for recursive solution, I think this solution should work for you.

function getIDs(values) {
  return values.reduce((acc, item) =&gt; {
    for (const [key, value] of Object.entries(item)) {
      if (key === &quot;fID&quot;) {
        acc.push(value);
      }
      
      if (Array.isArray(value)) {
        const subvalues = getIDs(value);
        acc = acc.concat(subvalues);
      }
    }
    
    return acc;
  }, []);
}


console.log(getIDs(detailsArray))

答案3

得分: 0

用正确的数据格式,你可以采用递归方法来获取嵌套的对象/数组。

const flat = o => o && typeof o === 'object'
    ? 'fID' in o
        ? o.fID
        : Object.values(o).flatMap(flat)
    : [];
const data = [
    {
        foldername: "myfolder",
        files: [
            { fID: "55", name: "file51" },
            {
                foldername: "mySubFolder",
                forms: [
                    { fID: "1111", name: "file01" }
                ],
                folder: []
            },
            {
                foldername: "mySubFolder02",
                forms: [
                    { fID: "4444", name: "file02" }
                ],
                folder: []
            }
        ]
    },
    {
        foldername: "myfolder2",
        files: [
            {
                foldername: "mySubFolder2",
                forms: [
                    { fID: "2222", name: "file03" }
                ],
                folder: []
            },
            {
                foldername: "mySubFolder03",
                forms: [
                    { fID: "3333", name: "file03" }
                ],
                folder: []
            }
        ]
    }
];
const result = data.flatMap(flat);

console.log(result);

上述代码使用递归方法来获取嵌套对象/数组中的"fID"属性值。

英文:

With proper data format, you could take a recursive approach for getting nested objects/arrays.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
flat = o => o && typeof o === 'object'
? 'fID' in o
? o.fID
: Object.values(o).flatMap(flat)
: [],
data = [{ foldername: "myfolder", files: [{ fID: "55", name: "file51" }, { foldername: "mySubFolder", forms: [{ fID: "1111", name: "file01" }], folder: [] }, { foldername: "mySubFolder02", forms: [{ fID: "4444", name: "file02" }], folder: [] }] }, { foldername: "myfolder2", files: [{ foldername: "mySubFolder2", forms: [{ fID: "2222", name: "file03" }], folder: [] }, { foldername: "mySubFolder03", forms: [{ fID: "3333", name: "file03" }], folder: [] }] }],
result = data.flatMap(flat);

console.log(result);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月9日 22:53:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686257.html
匿名

发表评论

匿名网友

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

确定