<!DOCTYPE html>
<html lang="en">
<head>
<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>
</head>
<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>
标签:解析,name,复杂,步骤,value,step,result,数据结构,children
From: https://www.cnblogs.com/it774274680/p/16977617.html