• 欢迎光临~

# 复杂数据结构的解析

``````<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<body>
<script>
var steps = [
{
name: "步骤1",
value: 0,
},
{
name: "步骤2",
value: 1,
},
{
name: "步骤3",
value: 2,
},
{
name: "步骤4",
children: [
[
{
name: "步骤4.1-a",
value: 3.1,
},
{
name: "步骤4.2-a",
value: 3.2,
},
],
[
{
name: "步骤4.1-b",
value: 3.1,
},
{
name: "步骤4.2-b",
value: 3.2,
},
{
name: "步骤4.3-b",
value: 3.3,
},
],
],
},
{
name: "步骤5",
value: 4,
},
];

function getAfterName(searchName) {
const result = [];
// searchName有可能是在没有children属性或者有children属性
let flag = false;
// 来标识是否匹配到children
let skip = false;
for (var i = 0; i < steps.length; i++) {
const step = steps[i];
if (step.children) {
for (var j = 0; j < step.children.length; j++) {
// debugger;
if (skip) {
continue;
}
// [{name:"zs",value:10},{name:'ls',value}]
const stepChildren = step.children[j];
for (var w = 0; w < stepChildren.length; w++) {
const child = stepChildren[w];

if (flag) {
result.push(child.name);
} else if (child.name == searchName) {
result.push(child.name);
flag = true;
skip = true;
}
}
}
} else {
if (flag) {
result.push(step.name);
}

if (step.name == searchName) {
result.push(step.name);
flag = true;
}
}
}
return result;
}
console.log(getAfterName("步骤3"));
console.log(getAfterName("步骤4.2-b"));
</script>
</body>
</html>

``````