簡體   English   中英

樹狀視圖的會計科目表

[英]Chart of accounts to tree view

我有這個字符串,它代表一個會計科目表

     "1: Comptes de capitaux
      10\. Capital et Réserves.
      101\. Capital.
      1011\. Capital souscrit - non appelé.
      1012\. Capital souscrit - appelé, non versé.
      1013\. Capital souscrit - appelé, versé.
      10131\. Capital non amorti.
      10132\. Capital amorti.
      1018\. Capital souscrit soumis à une réglementation particulière.
      105\. Ecarts de réévaluation.
      108\. Compte de l'exploitant. "

我想要這個 output:

    "1":
      { id:"1",
        accountName: "Comptes de capitaux",  
        children:{
         id:"10",
         accountName: "Capital et Réserves" ,
           children:{
             id:"101",
             accountName:"Capital",
               children: {
                 id:"1011",
                 accountName:"Capital souscrit - non appelé",
                 children: {...}  
               },{
                   id:"1012",
                   accountName:"Capital souscrit - appelé, non versé",
                   children: {...}
                 },...
                 },{
                   id:"105",
                   accountName:"Ecarts de réévaluation",
                   children: {}
                 },{...}  
           }
       }

該解決方案應該根據帳戶 ID 正確創建具有父子關系的樹結構,這是我的解決方案,它總是錯誤的,它沒有正確創建上面的樹結構,你能提供我正確的答案或我在我的錯誤中犯的錯誤嗎代碼

    function createChartOfAccountsTree(input) {
      // Split the input by line
      const lines = input.split("\n");
      // Create an object to store the accounts
      let accounts = {};
      // Loop through each line
      for (let i = 0; i < lines.length; i++) {
        // Split the line by space
        let parts = lines[i].split(" ");
        // Extract the ID and account name
        let id = parts[0];
        let accountName = parts.slice(1).join(" ");
        // remove the dot from account name
        accountName = accountName.replace(".","");
        // Create an account object
        let account = {
          id,
          accountName,
          children: {}
          };
        // Check if the account is a child of an existing account
        let parentId = id.slice(0, -1);
        let parent = accounts[parentId];
        if (parent) {
        // If the account has a parent, add it as a child
        parent.children[id] = account;
        } else {
          // If the account does not have a parent, it's a top level account
          accounts[id] = account;
       }
     }
    return accounts;
   }

console.log(createChartOfAccountsTree("1 : Comptes de capitaux\n10. Capital et    Réserves.\n101.Capital.\n1011. Capital souscrit - non appelé.\n1012. Capital souscrit - appelé, non versé.\n1013. Capital souscrit - appelé, versé.\n10131. Capital non amorti.\n10132. Capital amorti.\n1018. Capital souscrit soumis à une réglementation particulière.\n105. Ecarts de réévaluation.\n108. Compte de l'exploitant. ")); ```

有幾個問題:

  • 輸入似乎每行的格式都不相同。 在這個例子中,第一行在數字后面有一個冒號(或者一個空格和一個冒號——就像在你的代碼中一樣),而其他行在數字后面有一個點。

  • 在任何一種情況下,當它們緊跟在初始數字之后時,您當前的代碼都不會刪除這些定界符。 因此,當您只是從 id 中刪除最后一個字符以標識父級時,就沒有父子匹配——因為您只刪除了那個點或冒號(當它前面沒有空格時)。 如果輸入格式真的那么多變,那么您可以使用正則表達式來識別每一行中的兩個部分。

  • 該代碼似乎希望children是一個普通的 object ,其鍵是id值,但是您想要的 output 部分沒有指定這樣的鍵——它表示children就好像它是一個數組但語法無效。

  • 當您想用accounts[parentId]識別每個父母時,您必須確保每個條目都記錄在accounts中,而不僅僅是頂級。 所以應該修改else 也許使用一個單獨的變量來僅用於記錄頂級帳戶。

這是處理了上述問題的代碼:

 function createChartOfAccountsTree(input) { // Split the input by line, and immediately identify // the numeric prefix, and exclude final point const lines = input.matchAll(/^(\d+)[.:]+(.*?)\.?[ ]*$/gm); // Create an object to store all the accounts const accounts = {}; //...and one for the top-level accounts const top = {}; // Loop through each line, grabbing the parts for (const [, id, accountName] of lines) { // Create an account object let account = { id, accountName, children: {} }; // Check if the account is a child of an existing account let parentId = id.slice(0, -1); let parent = accounts[parentId]; if (parent) { // If the account has a parent, add it as a child parent.children[id] = account; } else { // If the account does not have a parent, it's a top level account top[id] = account; } // Log all accounts: accounts[id] = account; } return top; } const input = `1: Comptes de capitaux 10. Capital et Réserves. 101. Capital. 1011. Capital souscrit - non appelé. 1012. Capital souscrit - appelé, non versé. 1013. Capital souscrit - appelé, versé. 10131. Capital non amorti. 10132. Capital amorti. 1018. Capital souscrit soumis à une réglementation particulière. 105. Ecarts de réévaluation. 108. Compte de l'exploitant. `; console.log(createChartOfAccountsTree(input));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM