for example, there is such an array of objects
let data = [
{
title: "",
tagName: "h1"
},
{
title: "",
tagName: "h1"
},
{
title: "",
tagName: "h2"
},
{
title: "",
tagName: "h3"
},
{
title: "",
tagName: "h2"
},
{
title: "",
tagName: "h1"
},
{
title: "",
tagName: "h1"
},
{
title: "",
tagName: "h2"
},
{
title: "",
tagName: "h3"
},
{
title: "",
tagName: "h3"
}
]
it is required that according to the priority of tagName, it is arranged from H1 to h6. Each H1 followed by until the next H1 is its children node, which is pushed inward in turn. For example, after the above data is processed in this way, it will look like the following format:
let data = [
{
title: "",
tagName: "h1"
},
{
title: "",
tagName: "h1",
children: [
{
title: "",
tagName: "h2"
children: [
{
title: "",
tagName: "h3"
}
]
},
{
title: "",
tagName: "h2"
}
]
},
{
title: "",
tagName: "h1"
},
{
title: "",
tagName: "h1",
children: [
{
title: "",
tagName: "h2",
children: [
{
title: "",
tagName: "h3"
},
{
title: "",
tagName: "h3"
}
]
}
]
}
]
how to write such a traversal algorithm? Ask the boss for advice