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)
|