使用递归函数向数组动态添加新维度

我有一个这样的数组:


0: "SuSPENSE"

1: "Subcontractor Expense"

2: "Data Entry"

3: "Design"

4: "Programming"

5: "Subcontractor Expense - Other"

6: "Total Subcontractor Expense"

7: "Technology-Communication"

8: "Licenses"

9: "Domain Hosting Fee"

10: "Internet"

11: "Internet Servers"

12: "Internet - Other"

13: "Total Internet"

14: "Telephone"

15: "Call Center"

16: "Cellular Phones"

17: "Fax"

18: "Telephone - Other"

19: "Total Telephone"

20: "Hardware/Software"

21: "Computer Software"

22: "Hardware/Software - Other"

23: "Total Hardware/Software"

24: "Technology-Communication - Other"

25: "Total Technology-Communication"

这是类别和子类别的列表。例如, 是一个子类别,以 结尾。与 和 相同。模板始终相同,类别以“(名称)”开头,以“总计(名称)”结尾。但每个类别可以有许多级别的子类别,就像树一样。我正在尝试使用递归函数将此数组解析为类似JSON的数组或多维数组,但我永远不知道最大深度是多少。我尝试使用以下方法执行以下操作:"Subcontractor Expense""Total Subcontractor Expense""Internet""Total Internet"js


var parsedArray = [];

var items = getLineItems("Expense", "Total Expense"); //This is a function to return the mentioned array

var newArray = parseArray(items, "Expense");


function parseArray(items, category){

    var lineItems = [];

    for(var i = 0; i < items.length; i++){

        var inArray = $.inArray("Total " + items[i], items);

        if(inArray !== -1){

            parseArray(getLineItems(items[i], "Total " + items[i]), items[i]);

        }

        else {

            lineItems.push(items[i]);

        }

    }

    parsedArray[category] = lineItems;

}

但是这个递归函数永远不会比2级更深。有可能生成这样的东西吗?


"SuSPENSE"

"Subcontractor Expense"

    "Data Entry"

    "Design"

    "Programming"

"Subcontractor Expense - Other"

"Technology-Communication"

    "Licenses"

    "Domain Hosting Fee"

    "Internet"

        "Internet Servers"

        "Internet - Other"

    "Telephone"

        "Call Center"

        "Cellular Phones"

        "Fax"

        "Telephone - Other"

    "Hardware/Software"

        "Computer Software"

        "Hardware/Software - Other"

    "Technology-Communication - Other"


哈士奇WWW
浏览 106回答 2
2回答

忽然笑

我仍然在猜测你的输出格式。这是一个递归解决方案,给出了我在评论中询问的格式:[&nbsp; &nbsp; {name: "SuSPENSE"},&nbsp; &nbsp; {name: "Subcontractor Expense", children: [&nbsp; &nbsp; &nbsp; &nbsp; {name: "Data Entry"},&nbsp; &nbsp; &nbsp; &nbsp; {name: "Design"},&nbsp; &nbsp; &nbsp; &nbsp; {name: "Programming"},&nbsp; &nbsp; &nbsp; &nbsp; {name: "Subcontractor Expense - Other"}&nbsp; &nbsp; ]},&nbsp; &nbsp; {name: "Technology-Communication", children: [&nbsp; &nbsp; &nbsp; &nbsp;//...&nbsp; &nbsp; ]}]const restructure = (&nbsp; [s = undefined, ...ss],&nbsp;&nbsp; index = s == undefined ? -1 : ss .indexOf ('Total ' + s)) =>&nbsp;&nbsp; s == undefined&nbsp; &nbsp; ? []&nbsp; &nbsp; : index > -1&nbsp; &nbsp; &nbsp; ? [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {name: s, children: restructure (ss .slice (0, index))},&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ... restructure (ss .slice (index + 1))&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; : [{name: s}, ... restructure (ss)]const data = ["SuSPENSE", "Subcontractor Expense", "Data Entry", "Design", "Programming", "Subcontractor Expense - Other", "Total Subcontractor Expense", "Technology-Communication", "Licenses", "Domain Hosting Fee", "Internet", "Internet Servers", "Internet - Other", "Total Internet", "Telephone", "Call Center", "Cellular Phones", "Fax", "Telephone - Other", "Total Telephone", "Hardware/Software", "Computer Software", "Hardware/Software - Other", "Total Hardware/Software", "Technology-Communication - Other", "Total Technology-Communication"]console .log (&nbsp; restructure (data)).as-console-wrapper {min-height: 100% !important; top: 0}请注意,在其中一个递归情况下,我们调用 main 函数两次。一次用于嵌套数据,一次用于数组的其余部分。另一种可能的结构,一个我不太喜欢的结构,但可能满足您的需求,看起来像这样:[&nbsp; &nbsp; "SuSPENSE",&nbsp; &nbsp; {"Subcontractor Expense": [&nbsp; &nbsp; &nbsp; &nbsp; "Data Entry", "Design", "Programming", "Subcontractor Expense - Other"&nbsp; &nbsp; ]},&nbsp; &nbsp; { "Technology-Communication": [&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; ]}]这可以通过一个小的修改来实现:const restructure = (&nbsp; [s = undefined, ...ss],&nbsp;&nbsp; index = s == undefined ? -1 : ss .indexOf ('Total ' + s)) =>&nbsp;&nbsp; s == undefined&nbsp; &nbsp; ? []&nbsp; : index > -1&nbsp; &nbsp; ? [&nbsp; &nbsp; &nbsp; &nbsp; {[s]: restructure (ss .slice (0, index))},&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ... restructure (ss .slice (index + 1))&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; : [s, ... restructure (ss)]更新这个变体与第一个变体相同,但对于不习惯我的表达方式的人来说,可能看起来更熟悉:const restructure = ([s = undefined, ...ss]) => {&nbsp; if (s == undefined) {return []}&nbsp; const index = ss .indexOf ('Total ' + s)&nbsp; return index < 0&nbsp; &nbsp; ? [{name: s}, ... restructure (ss)]&nbsp; &nbsp; : [&nbsp; &nbsp; &nbsp; &nbsp; {name: s, children: restructure (ss .slice (0, index))},&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ... restructure (ss .slice (index + 1))&nbsp; &nbsp; &nbsp; ]}

幕布斯7119047

您可以通过检查当前元素是否具有以单词开头,然后继续使用当前元素的文本的相应元素来执行此操作,如果是,则递增级别。当当前元素以单词总计开头时,您将递减级别。Totalconst data = {"0":"SuSPENSE","1":"Subcontractor Expense","2":"Data Entry","3":"Design","4":"Programming","5":"Subcontractor Expense - Other","6":"Total Subcontractor Expense","7":"Technology-Communication","8":"Licenses","9":"Domain Hosting Fee","10":"Internet","11":"Internet Servers","12":"Internet - Other","13":"Total Internet","14":"Telephone","15":"Call Center","16":"Cellular Phones","17":"Fax","18":"Telephone - Other","19":"Total Telephone","20":"Hardware/Software","21":"Computer Software","22":"Hardware/Software - Other","23":"Total Hardware/Software","24":"Technology-Communication - Other","25":"Total Technology-Communication"}function toNested(data) {&nbsp; const result = [];&nbsp; const levels = [result]&nbsp; let level = 0;&nbsp; const checkChildren = (string, data) => {&nbsp; &nbsp; return data.some(e => e === `Total ${string}`)&nbsp; }&nbsp; data.forEach((e, i) => {&nbsp; &nbsp; const object = { name: e, children: []}&nbsp; &nbsp; levels[level + 1] = object.children;&nbsp; &nbsp; if (e.startsWith('Total')) level--;&nbsp; &nbsp; else levels[level].push(object);&nbsp; &nbsp; if (checkChildren(e, data.slice(i))) level++;&nbsp; })&nbsp; return result;}const result = toNested(Object.values(data));console.log(result)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript