英文:
Recursive function to restructure an array of object
问题
I'm currently working on displaying item.
But I'm stuck because the data structure is super nested.
This is the original structure:
[
{
"key": "name",
"value": "Johnweak",
"title": "name",
"type": "input"
},
{
"key": "lastname",
"value": "weak",
"title": "lastname",
"type": "input"
},
{
"key": "cert",
"value": "Certificate",
"title": "Certificate",
"type": "object",
"children": [
{
"key": "cert1",
"value": "cert1",
"title": "Certificate 1",
"type": "object",
"children": [
{
"key": "cert1uni",
"value": "cert1uni",
"title": "Cert 1 University name",
"type": "input"
},
{
"key": "cert1cgpa",
"value": "cert1cpga",
"title": "Cert 1 CGPA",
"type": "input"
}
]
},
{
"key": "cert2",
"value": "cert2",
"title": "Certificate 2",
"type": "object",
"children": [
{
"key": "cert2uni",
"value": "cert2uni",
"title": "Cert 2 University name",
"type": "input"
},
{
"key": "cert2cgpa",
"value": "cert2cgpa",
"title": "Cert 2 CGPA",
"type": "input"
}
]
}
]
},
{
"key": "dob",
"value": "2022-02-31",
"title": "Date of birth",
"type": "dropdown"
}
]
So in this case, I'm supposed to make a recursive function to loop through it and restructure it into a new array of objects such as:
[
{
"key": "name",
"value": "Johnweak",
"title": "name",
"type": "input"
},
{
"key": "lastname",
"value": "weak",
"title": "lastname",
"type": "input"
},
{
"key": "cert1uni",
"value": "cert1uni",
"title": "Cert 1 University name",
"type": "input"
},
{
"key": "cert1cgpa",
"value": "cert1cpga",
"title": "Cert 1 CGPA",
"type": "input"
},
{
"key": "cert2uni",
"value": "cert2uni",
"title": "Cert 2 University name",
"type": "input"
},
{
"key": "cert2cgpa",
"value": "cert2cpga",
"title": "Cert 2 CGPA",
"type": "input"
},
{
"key": "dob",
"value": "2022-02-31",
"title": "Date of birth",
"type": "dropdown"
}
]
This is my current code.
const nestedArray = (array) => {
let A = [];
array.map((item) => {
if (item.type === "object" && item.children) {
Object.assign(A, item.children);
nestedArray(A);
} else {
Object.assign(item);
}
});
}
But it doesn't really work, though. Does anyone know how to fix it?
英文:
I'm currently working on displaying item.
But im stuck because the data structure is super nested.
This is the original structure:
[
{
"key": "name",
"value": "Johnweak",
"title": "name",
"type": "input"
},
{
"key": "lastname",
"value": "weak",
"title": "lastname",
"type": "input"
},
{
"key": "cert",
"value": "Certificate",
"title": "Certificate",
"type": "object",
"children": [
{
"key": "cert1",
"value": "cert1",
"title": "Certificate 1",
"type": "object",
"children": [
{
"key": "cert1uni",
"value": "cert1uni",
"title": "Cert 1 University name",
"type": "input"
},
{
"key": "cert1cgpa",
"value": "cert1cpga",
"title": "Cert 1 CGPA",
"type": "input"
}
]
},
{
"key": "cert2",
"value": "cert2",
"title": "Certificate 2",
"type": "object",
"children": [
{
"key": "cert2uni",
"value": "cert2uni",
"title": "Cert 2 University name",
"type": "input"
},
{
"key": "cert2cgpa",
"value": "cert2cgpa",
"title": "Cert 2 CGPA",
"type": "input"
}
]
}
]
},
{
"key": "dob",
"value": "2022-02-31",
"title": "Date of birth",
"type": "dropdown"
}
]
So in this case, Im supposed to make a recursive function to loop thru it and restructure to a new array of object such as:
[
{
"key": "name",
"value": "Johnweak",
"title": "name",
"type": "input"
},
{
"key": "lastname",
"value": "weak",
"title": "lastname",
"type": "input"
},
{
"key": "cert1uni",
"value": "cert1uni",
"title": "Cert 1 University name",
"type": "input"
},
{
"key": "cert1cgpa",
"value": "cert1cpga",
"title": "Cert 1 CGPA",
"type": "input"
},
{
"key": "cert2uni",
"value": "cert2uni",
"title": "Cert 2 University name",
"type": "input"
},
{
"key": "cert2cgpa",
"value": "cert2cpga",
"title": "Cert 2 CGPA",
"type": "input"
},
{
"key": "dob",
"value": "2022-02-31",
"title": "Date of birth",
"type": "dropdown"
}
]
this is my current code.
const nestedArray = (array) => {
let A = [];
array.map((item) => {
if((item.type === "object" && item.children) {
Object.assign(A, item.children);
nestedArray(A);
} else {
Object.assign(item);
}
}
}
But it doesn't really work tho
Does anyone know how to fix it?
答案1
得分: 1
要跳过作为其他项的父项,并仅累积叶子(非父项)到一个单一数组中:
```ts
function nestedArray(array) {
return array.flatMap((item) => {
if (item.type === "object" && item.children) {
return nestedArray(item.children);
} else {
return item; // 叶子(非父项)
}
});
}
<details>
<summary>英文:</summary>
You want to skip items that are parent of others, and accumulate only leaves (non parent items) in a single array:
```ts
function nestedArray(array) {
return array.flatMap((item) => {
if ((item.type === "object" && item.children) {
return nestedArray(item.children);
} else {
return item; // Leaf (non parent)
}
});
}
答案2
得分: 1
const nestedArray = (arr, output = [], depth = 0) => {
arr.forEach((item) => {
if(item.type === "object" && item.children) {
depth += 1;
output.push(nestedArray(item.children));
} else {
output.push(item);
}
});
return output.flat(depth);
}
const arr = [
{
"key": "name",
"value": "Johnweak",
"title": "name",
"type": "input"
},
{
"key": "lastname",
"value": "weak",
"title": "lastname",
"type": "input"
},
{
"key": "cert",
"value": "Certificate",
"title": "Certificate",
"type": "object",
"children": [
{
"key": "cert1",
"value": "cert1",
"title": "Certificate 1",
"type": "object",
"children": [
{
"key": "cert1uni",
"value": "cert1uni",
"title": "Cert 1 University name",
"type": "input"
},
{
"key": "cert1cgpa",
"value": "cert1cpga",
"title": "Cert 1 CGPA",
"type": "input"
}
]
},
{
"key": "cert2",
"value": "cert2",
"title": "Certificate 2",
"type": "object",
"children": [
{
"key": "cert2uni",
"value": "cert2uni",
"title": "Cert 2 University name",
"type": "input"
},
{
"key": "cert2cgpa",
"value": "cert2cgpa",
"title": "Cert 2 CGPA",
"type": "input"
}
]
},
{
"key": "cert3",
"value": "cert3",
"title": "Certificate 3",
"type": "object",
"children": [
{
"key": "cert3uni",
"value": "cert3uni",
"title": "Cert 3 University name",
"type": "input"
},
{
"key": "cert3cgpa",
"value": "cert3cgpa",
"title": "Cert 3 CGPA",
"type": "input"
}
]
}
]
},
{
"key": "dob",
"value": "2022-02-31",
"title": "Date of birth",
"type": "dropdown"
}
];
console.log(nestedArray(arr));
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const nestedArray = (arr, output = [], depth = 0) => {
arr.forEach((item) => {
if(item.type === "object" && item.children) {
depth +=1
output.push(nestedArray(item.children))
} else {
output.push(item)
}
})
return output.flat(depth);
}
const arr = [
{
"key": "name",
"value": "Johnweak",
"title": "name",
"type": "input"
},
{
"key": "lastname",
"value": "weak",
"title": "lastname",
"type": "input"
},
{
"key": "cert",
"value": "Certificate",
"title": "Certificate",
"type": "object",
"children": [
{
"key": "cert1",
"value": "cert1",
"title": "Certificate 1",
"type": "object",
"children": [
{
"key": "cert1uni",
"value": "cert1uni",
"title": "Cert 1 University name",
"type": "input"
},
{
"key": "cert1cgpa",
"value": "cert1cpga",
"title": "Cert 1 CGPA",
"type": "input"
}
]
},
{
"key": "cert2",
"value": "cert2",
"title": "Certificate 2",
"type": "object",
"children": [
{
"key": "cert2uni",
"value": "cert2uni",
"title": "Cert 2 University name",
"type": "input"
},
{
"key": "cert2cgpa",
"value": "cert2cgpa",
"title": "Cert 2 CGPA",
"type": "input"
},
{
"key": "cert3",
"value": "cert3",
"title": "Certificate 3",
"type": "object",
"children": [
{
"key": "cert2uni",
"value": "cert2uni",
"title": "Cert 3 University name",
"type": "input"
},
{
"key": "cert3cgpa",
"value": "cert3cgpa",
"title": "Cert 3 CGPA",
"type": "input"
}
]
}
]
}
]
},
{
"key": "dob",
"value": "2022-02-31",
"title": "Date of birth",
"type": "dropdown"
}
];
console.log(nestedArray(arr))
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论