这个是工作中遇到的一个坑,解决之后记录一下

题目

原有数组中 looploopend 为一组,loopcondloopcondend 为一组,将每组之间的数组整合为一个新的二维数组添加到 looploopcond 的第三位。(可能出现各种并列嵌套关系,但 loop 一定在 loopcond 外层)

↓ 举个简单的栗子 ↓

举例

1
2
3
4
5
6
7
8
9
10
11
12
[
['aaaa', '', ''],
['loop', '', ''],
['bbbb', '', ''],
['loopcond', '', ''],
['cccc', '', ''],
['loopcondend', '', ''],
['loopend', '', ''],
['loop', '', ''],
['dddd', '', ''],
['loopend', '', ''],
]

将转化为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[
['aaaa', '', ''],
['loop', '', '', [
['bbbb', '', ''],
['loopcond', '', '', [
['cccc', '', ''],
]],
['loopcondend', '', ''],
]],
['loopend', '', ''],
['loop', '', '', [
['dddd', '', ''],
]],
['loopend', '', ''],
]

思路

Ps:由于 looploopcond 算法基本相同,因此这里只拿 loop 举例

目前的难点在于如何确定 looploopend 的对应关系。

解决方法就是在第一次遍历中,记录下 looploopend 出现的次数以及索引,当两个次数相等时,我们就可以确定这是一个对应关系了。然后将中间的数组整理成一个新数组,push到之前的 loop 数组中,再进行递归。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
let _loopResult = [
[
['sdfasfda', '', ''],
['loop', '', ''],
['ddddd1', '', ''],
['ddddd2', '', ''],
['ddddd3', '', ''],
['loop', '', ''],
['loop', '', ''],
['3', ''],
['loop', '', ''],
['4', ''],
['loopend', '', ''],
['loopend', '', ''],
['aaaaa1', '', ''],
['aaaaa2', '', ''],
['aaaaa3', '', ''],
['loopend', '', ''],
['cccccc1', '', ''],
['cccccc2', '', ''],
['cccccc3', '', ''],
['cccccc4', '', ''],
['cccccc5', '', ''],
['loop', '', ''],
['bbbbbb1', '', ''],
['bbbbbb2', '', ''],
['bbbbbb3', '', ''],
['loopend', '', ''],
['loopend', '', ''],
['loop', '', ''],
['loopcond', ''],
['eeeeee0', '', ''],
['loopcond', ''],
['eeeeee1', '', ''],
['eeeeee2', '', ''],
['eeeeee3', '', ''],
['loopcondend'],
['eeeeeee4', '', ''],
['loopcond', ''],
['eeeeee5', '', ''],
['eeeeee6', '', ''],
['eeeeee7', '', ''],
['loopcondend'],
['loopcondend'],
['loopend', '', ''],
],
];
const doLoopResult = function(loopStartName, loopEndName, arr) {
const arrSum = (_arr) => {
let sum = 0;
for (let i = 0; i < _arr.length; i++) {
sum += _arr[i];
}
return sum;
}
let loopReturnIndex = [];
let loopendReturnIndex = [];
let loopIndexResult = [];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
if (arr[i][j][0] === loopStartName) {
loopReturnIndex.push(j);
} else if (arr[i][j][0] === loopEndName) {
loopendReturnIndex.push(j);
}
}
}
for (let i = 0; i < loopReturnIndex.length; i++) {
loopIndexResult.push(loopendReturnIndex[i] - loopReturnIndex[i]);
}
if (arrSum(loopIndexResult) === loopIndexResult.length) {
return arr;
}

let loopNum = 0;
let loopendNum = 0;
let loopIndexBegin = [];
let loopIndexEnd = [];
let loopItem = [];
let loopItemIndex = 0;
let i = 0;
let j = 0;
for (; i < arr.length; i++) {
for (; j < arr[i].length; j++) {
if (arr[i][j]) {
if (arr[i][j][0] === loopStartName) {
if (loopIndexBegin.length === 0) {
loopItem.push([]);
arr[i][j].push(loopItem[loopItemIndex])
}
loopNum++;
loopIndexBegin.push(j);
} else if (arr[i][j][0] === loopEndName) {
loopendNum++;
loopIndexEnd.push(j);
}
}
if (loopNum === loopendNum && loopNum !== 0) {
if (loopNum !== 1) {
loopItem.splice(loopItemIndex + 1, loopItem.length)
}
for (let k = 0; k < loopItem.length; k++) {
for (let m = 0; m < arr.length; m++) {
for (let n = loopIndexBegin[0] + 1; n < loopIndexEnd[loopIndexEnd.length - 1]; n++) {
loopItem[loopItemIndex].push(arr[m][n]);
}
arr[m].splice(loopIndexBegin[0] + 1, loopIndexEnd[loopIndexEnd.length - 1] - loopIndexBegin[0] - 1);
j -= (loopIndexEnd[loopIndexEnd.length - 1] - loopIndexBegin[0] - 1);
loopIndexBegin = [];
loopIndexEnd = [];
break;
}
loopItemIndex++;
}
loopNum = 0;
loopendNum = 0;
}
}
}
return doLoopResult(loopStartName, loopEndName , loopItem);
}
doLoopResult('loopcond', 'loopcondend', _loopResult);
doLoopResult('loop', 'loopend', _loopResult);
console.log(_loopResult)

最后得到结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
[
[
["sdfasfda", "", ""],
["loop", "", "", [
["ddddd1", "", ""],
["ddddd2", "", ""],
["ddddd3", "", ""],
["loop", "", "", [
["loop", "", "", [
["3", ""],
["loop", "", "", [
["4", ""]
]],
["loopend", "", ""]
]],
["loopend", "", ""],
["aaaaa1", "", ""],
["aaaaa2", "", ""],
["aaaaa3", "", ""]
]],
["loopend", "", ""],
["cccccc1", "", ""],
["cccccc2", "", ""],
["cccccc3", "", ""],
["cccccc4", "", ""],
["cccccc5", "", ""],
["loop", "", "", [
["bbbbbb1", "", ""],
["bbbbbb2", "", ""],
["bbbbbb3", "", ""]
]],
["loopend", "", ""]
]],
["loopend", "", ""],
["loop", "", "", [
["loopcond", "", [
["eeeeee0", "", ""],
["loopcond", "", [
["eeeeee1", "", ""],
["eeeeee2", "", ""],
["eeeeee3", "", ""]
]],
["loopcondend"],
["eeeeeee4", "", ""],
["loopcond", "", [
["eeeeee5", "", ""],
["eeeeee6", "", ""],
["eeeeee7", "", ""]
]],
["loopcondend"]
]],
["loopcondend"]
]],
["loopend", "", ""]
]
]

总结

问题虽然解决了,但是五个嵌套的循环看着就很伤。如果数据规模稍微大一点估计就炸了,以后有时间大概需要再想一个更好的办法了。