亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用遞歸函數向數組動態添加新維度

使用遞歸函數向數組動態添加新維度

哈士奇WWW 2022-09-23 21:59:17
我有一個這樣的數組: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"jsvar parsedArray = [];var items = getLineItems("Expense", "Total Expense"); //This is a function to return the mentioned arrayvar 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"
查看完整描述

2 回答

?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

我仍然在猜測你的輸出格式。這是一個遞歸解決方案,給出了我在評論中詢問的格式:


[

    {name: "SuSPENSE"},

    {name: "Subcontractor Expense", children: [

        {name: "Data Entry"},

        {name: "Design"},

        {name: "Programming"},

        {name: "Subcontractor Expense - Other"}

    ]},

    {name: "Technology-Communication", children: [

       //...

    ]}

]

const restructure = (

  [s = undefined, ...ss], 

  index = s == undefined ? -1 : ss .indexOf ('Total ' + s)

) => 

  s == undefined

    ? []

    : index > -1

      ? [

          {name: s, children: restructure (ss .slice (0, index))}, 

          ... restructure (ss .slice (index + 1))

        ]

      : [{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 (

  restructure (data)

)

.as-console-wrapper {min-height: 100% !important; top: 0}

請注意,在其中一個遞歸情況下,我們調用 main 函數兩次。一次用于嵌套數據,一次用于數組的其余部分。


另一種可能的結構,一個我不太喜歡的結構,但可能滿足您的需求,看起來像這樣:


[

    "SuSPENSE",

    {"Subcontractor Expense": [

        "Data Entry", "Design", "Programming", "Subcontractor Expense - Other"

    ]},

    { "Technology-Communication": [ 

        // ...

    ]}

]

這可以通過一個小的修改來實現:


const restructure = (

  [s = undefined, ...ss], 

  index = s == undefined ? -1 : ss .indexOf ('Total ' + s)

) => 

  s == undefined

    ? []

  : index > -1

    ? [

        {[s]: restructure (ss .slice (0, index))}, 

        ... restructure (ss .slice (index + 1))

      ]

    : [s, ... restructure (ss)]

更新

這個變體與第一個變體相同,但對于不習慣我的表達方式的人來說,可能看起來更熟悉:


const restructure = ([s = undefined, ...ss]) => {

  if (s == undefined) {return []}

  const index = ss .indexOf ('Total ' + s)

  return index < 0

    ? [{name: s}, ... restructure (ss)]

    : [

        {name: s, children: restructure (ss .slice (0, index))}, 

        ... restructure (ss .slice (index + 1))

      ]

}


查看完整回答
反對 回復 2022-09-23
?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

您可以通過檢查當前元素是否具有以單詞開頭,然后繼續使用當前元素的文本的相應元素來執行此操作,如果是,則遞增級別。當當前元素以單詞總計開頭時,您將遞減級別。Total


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

  const result = [];

  const levels = [result]

  let level = 0;


  const checkChildren = (string, data) => {

    return data.some(e => e === `Total ${string}`)

  }


  data.forEach((e, i) => {

    const object = { name: e, children: []}

    levels[level + 1] = object.children;


    if (e.startsWith('Total')) level--;

    else levels[level].push(object);


    if (checkChildren(e, data.slice(i))) level++;

  })


  return result;

}


const result = toNested(Object.values(data));

console.log(result)


查看完整回答
反對 回復 2022-09-23
  • 2 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號