递归函数用于重组对象数组

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

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) =&gt; {
if ((item.type === &quot;object&quot; &amp;&amp; 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) =&gt; {
arr.forEach((item) =&gt; {
if(item.type === &quot;object&quot; &amp;&amp; item.children) {
depth +=1
output.push(nestedArray(item.children))  
} else {
output.push(item)
}
})
return output.flat(depth);
}
const arr = [
{
&quot;key&quot;: &quot;name&quot;,
&quot;value&quot;: &quot;Johnweak&quot;,
&quot;title&quot;: &quot;name&quot;,
&quot;type&quot;: &quot;input&quot;
},
{
&quot;key&quot;: &quot;lastname&quot;,
&quot;value&quot;: &quot;weak&quot;,
&quot;title&quot;: &quot;lastname&quot;,
&quot;type&quot;: &quot;input&quot;
},
{
&quot;key&quot;: &quot;cert&quot;,
&quot;value&quot;: &quot;Certificate&quot;,
&quot;title&quot;: &quot;Certificate&quot;,
&quot;type&quot;: &quot;object&quot;,
&quot;children&quot;: [
{
&quot;key&quot;: &quot;cert1&quot;,
&quot;value&quot;: &quot;cert1&quot;,
&quot;title&quot;: &quot;Certificate 1&quot;,
&quot;type&quot;: &quot;object&quot;,
&quot;children&quot;: [
{
&quot;key&quot;: &quot;cert1uni&quot;,
&quot;value&quot;: &quot;cert1uni&quot;,
&quot;title&quot;: &quot;Cert 1 University name&quot;,
&quot;type&quot;: &quot;input&quot;
},
{
&quot;key&quot;: &quot;cert1cgpa&quot;,
&quot;value&quot;: &quot;cert1cpga&quot;,
&quot;title&quot;: &quot;Cert 1 CGPA&quot;,
&quot;type&quot;: &quot;input&quot;
}
]
},
{
&quot;key&quot;: &quot;cert2&quot;,
&quot;value&quot;: &quot;cert2&quot;,
&quot;title&quot;: &quot;Certificate 2&quot;,
&quot;type&quot;: &quot;object&quot;,
&quot;children&quot;: [
{
&quot;key&quot;: &quot;cert2uni&quot;,
&quot;value&quot;: &quot;cert2uni&quot;,
&quot;title&quot;: &quot;Cert 2 University name&quot;,
&quot;type&quot;: &quot;input&quot;
},
{
&quot;key&quot;: &quot;cert2cgpa&quot;,
&quot;value&quot;: &quot;cert2cgpa&quot;,
&quot;title&quot;: &quot;Cert 2 CGPA&quot;,
&quot;type&quot;: &quot;input&quot;
},
{
&quot;key&quot;: &quot;cert3&quot;,
&quot;value&quot;: &quot;cert3&quot;,
&quot;title&quot;: &quot;Certificate 3&quot;,
&quot;type&quot;: &quot;object&quot;,
&quot;children&quot;: [
{
&quot;key&quot;: &quot;cert2uni&quot;,
&quot;value&quot;: &quot;cert2uni&quot;,
&quot;title&quot;: &quot;Cert 3 University name&quot;,
&quot;type&quot;: &quot;input&quot;
},
{
&quot;key&quot;: &quot;cert3cgpa&quot;,
&quot;value&quot;: &quot;cert3cgpa&quot;,
&quot;title&quot;: &quot;Cert 3 CGPA&quot;,
&quot;type&quot;: &quot;input&quot;
}
]
}
]
}
]
},
{
&quot;key&quot;: &quot;dob&quot;,
&quot;value&quot;: &quot;2022-02-31&quot;,
&quot;title&quot;: &quot;Date of birth&quot;,
&quot;type&quot;: &quot;dropdown&quot;
}
];
console.log(nestedArray(arr))

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月23日 20:59:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545167.html
匿名

发表评论

匿名网友

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

确定