英文:
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":[
]
},
{
"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":[
]
},
],
},
]
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 > 0)
allFormIds = el.folder.map(f => f.fID)
})
Still I couldn'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: "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);
<!-- end snippet -->
答案2
得分: 0
正如您要求的递归解决方案,我认为这个解决方案对您应该有效。
英文:
As you asked for recursive solution, I think this solution should work for you.
function getIDs(values) {
return values.reduce((acc, item) => {
for (const [key, value] of Object.entries(item)) {
if (key === "fID") {
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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论