2013-03-17 53 views
0

我有以下JSON數組:移除元件遞歸

{ 
"id": "1004482725", 
"parent": 0, 
"industry": "Akrotiri", 
"industry_id": "root", 
"name": "Comp", 
"percentage": "0", 
"report": "null", 
"description": "", 
"subsidiary": "3", 
"children": [{ 
    "id": "1004482733", 
    "parent": "1004482725", 
    "industry": "Akrotiri", 
    "industry_id": "industry9", 
    "name": "Comp11", 
    "percentage": "30", 
    "report": "null", 
    "description": "", 
    "subsidiary": "1", 
    "children": [{ 
     "id": "1004482734", 
     "parent": "1004482733", 
     "industry": "Akrotiri", 
     "industry_id": "industry10", 
     "name": "Comp110", 
     "percentage": "30.00", 
     "report": "null", 
     "description": "", 
     "subsidiary": "1", 
     "children": [{ 
      "id": "aa02634", 
      "parent": "1004482734", 
      "industry": "", 
      "industry_id": "0", 
      "name": "Sef 4", 
      "percentage": "4", 
      "report": "null", 
      "description": "", 
      "subsidiary": "0", 
      "children": [] 
     }] 
    }, { 
     "id": "1004482735", 
     "parent": "1004482733", 
     "industry": "Akrotiri", 
     "industry_id": "industry11", 
     "name": "Comp111", 
     "percentage": "30.00", 
     "report": "null", 
     "description": "", 
     "subsidiary": "1", 
     "children": [{ 
      "id": "aa02635", 
      "parent": "1004482735", 
      "industry": "", 
      "industry_id": "0", 
      "name": "Sef 5", 
      "percentage": "5", 
      "report": "null", 
      "description": "", 
      "subsidiary": "0", 
      "children": [] 
     }] 
    }, { 
     "id": "1004482736", 
     "parent": "1004482733", 
     "industry": "Akrotiri", 
     "industry_id": "industry12", 
     "name": "Comp112", 
     "percentage": "30.00", 
     "report": "null", 
     "description": "", 
     "subsidiary": "1", 
     "children": [{ 
      "id": "1004482737", 
      "parent": "1004482736", 
      "industry": "Akrotiri", 
      "industry_id": "industry13", 
      "name": "Comp1120", 
      "percentage": "100.00", 
      "report": "null", 
      "description": "", 
      "subsidiary": "2", 
      "children": [{ 
       "id": "1004482738", 
       "parent": "1004482737", 
       "industry": "Akrotiri", 
       "industry_id": "industry14", 
       "name": "Comp11200", 
       "percentage": "100.00", 
       "report": "null", 
       "description": "", 
       "subsidiary": "1", 
       "children": [{ 
        "id": "1004482739", 
        "parent": "1004482738", 
        "industry": "Akrotiri", 
        "industry_id": "industry15", 
        "name": "Comp112000", 
        "percentage": "100.00", 
        "report": "null", 
        "description": "", 
        "subsidiary": "1", 
        "children": [{ 
         "id": "aa02636", 
         "parent": "1004482739", 
         "industry": "", 
         "industry_id": "0", 
         "name": "Sef 6", 
         "percentage": "6", 
         "report": "null", 
         "description": "", 
         "subsidiary": "0", 
         "children": [] 
        }] 
       }] 
      }] 
     }, { 
      "id": "1004482740", 
      "parent": "1004482736", 
      "industry": "Akrotiri", 
      "industry_id": "industry16", 
      "name": "Comp1121", 
      "percentage": "100.00", 
      "report": "null", 
      "description": "", 
      "subsidiary": "1", 
      "children": [{ 
       "id": "1004482741", 
       "parent": "1004482740", 
       "industry": "Akrotiri", 
       "industry_id": "industry17", 
       "name": "Comp11210", 
       "percentage": "100.00", 
       "report": "null", 
       "description": "", 
       "subsidiary": "1", 
       "children": [{ 
        "id": "aa02637", 
        "parent": "1004482741", 
        "industry": "", 
        "industry_id": "0", 
        "name": "Sef 7", 
        "percentage": "7", 
        "report": "null", 
        "description": "", 
        "subsidiary": "0", 
        "children": [] 
       }] 
      }] 
     }, { 
      "id": "1004482742", 
      "parent": "1004482736", 
      "industry": "Akrotiri", 
      "industry_id": "industry18", 
      "name": "Comp1122", 
      "percentage": "30.00", 
      "report": "null", 
      "description": "", 
      "subsidiary": "2", 
      "children": [{ 
       "id": "1004482743", 
       "parent": "1004482742", 
       "industry": "Akrotiri", 
       "industry_id": "industry19", 
       "name": "Comp11220", 
       "percentage": "100.00", 
       "report": "null", 
       "description": "", 
       "subsidiary": "1", 
       "children": [{ 
        "id": "aa02638", 
        "parent": "1004482743", 
        "industry": "", 
        "industry_id": "0", 
        "name": "Sef 8", 
        "percentage": "8", 
        "report": "null", 
        "description": "", 
        "subsidiary": "0", 
        "children": [] 
       }] 
      }] 
     }] 
    }] 
}, { 
    "id": "1004482726", 
    "parent": "1004482725", 
    "industry": "Akrotiri", 
    "industry_id": "industry2", 
    "name": "Comp10", 
    "percentage": "30", 
    "report": "null", 
    "description": "", 
    "subsidiary": "1", 
    "children": [{ 
     "id": "1004482732", 
     "parent": "1004482726", 
     "industry": "Akrotiri", 
     "industry_id": "industry8", 
     "name": "Comp102", 
     "percentage": "30", 
     "report": "null", 
     "description": "", 
     "subsidiary": "1", 
     "children": [{ 
      "id": "aa02633", 
      "parent": "1004482732", 
      "industry": "", 
      "industry_id": "0", 
      "name": "Sef 3", 
      "percentage": "3", 
      "report": "null", 
      "description": "", 
      "subsidiary": "0", 
      "children": [] 
     }] 
    }, { 
     "id": "1004482728", 
     "parent": "1004482726", 
     "industry": "Akrotiri", 
     "industry_id": "industry4", 
     "name": "Comp101", 
     "percentage": "30", 
     "report": "null", 
     "description": "", 
     "subsidiary": "1", 
     "children": [{ 
      "id": "1004482729", 
      "parent": "1004482728", 
      "industry": "Akrotiri", 
      "industry_id": "industry5", 
      "name": "Comp1010", 
      "percentage": "100.00", 
      "report": "null", 
      "description": "", 
      "subsidiary": "1", 
      "children": [{ 
       "id": "1004482730", 
       "parent": "1004482729", 
       "industry": "Akrotiri", 
       "industry_id": "industry6", 
       "name": "Comp10100", 
       "percentage": "100.00", 
       "report": "null", 
       "description": "", 
       "subsidiary": "1", 
       "children": [{ 
        "id": "1004482731", 
        "parent": "1004482730", 
        "industry": "Akrotiri", 
        "industry_id": "industry7", 
        "name": "Comp101000", 
        "percentage": "100.00", 
        "report": "null", 
        "description": "", 
        "subsidiary": "1", 
        "children": [{ 
         "id": "aa02632", 
         "parent": "1004482731", 
         "industry": "", 
         "industry_id": "0", 
         "name": "Sef 2", 
         "percentage": "2", 
         "report": "null", 
         "description": "", 
         "subsidiary": "0", 
         "children": [] 
        }] 
       }] 
      }] 
     }] 
    }, { 
     "id": "1004482727", 
     "parent": "1004482726", 
     "industry": "Akrotiri", 
     "industry_id": "industry3", 
     "name": "Comp100", 
     "percentage": "30", 
     "report": "null", 
     "description": "", 
     "subsidiary": "1", 
     "children": [{ 
      "id": "aa02631", 
      "parent": "1004482727", 
      "industry": "", 
      "industry_id": "0", 
      "name": "Sef1", 
      "percentage": "1", 
      "report": "null", 
      "description": "", 
      "subsidiary": "0", 
      "children": [] 
     }] 
    }] 
}, { 
    "id": "1004482744", 
    "parent": "1004482725", 
    "industry": "Akrotiri", 
    "industry_id": "industry20", 
    "name": "Comp12", 
    "percentage": "30", 
    "report": "null", 
    "description": "", 
    "subsidiary": "1", 
    "children": [{ 
     "id": "1004482745", 
     "parent": "1004482744", 
     "industry": "Akrotiri", 
     "industry_id": "industry21", 
     "name": "Comp120", 
     "percentage": "30.00", 
     "report": "null", 
     "description": "", 
     "subsidiary": "1", 
     "children": [{ 
      "id": "", 
      "parent": "", 
      "industry": "", 
      "industry_id": "", 
      "name": "", 
      "percentage": "", 
      "report": "", 
      "description": "", 
      "subsidiary": "", 
      "children": "" 
     }, { 
      "id": "1004482748", 
      "parent": "1004482745", 
      "industry": "Akrotiri", 
      "industry_id": "industry24", 
      "name": "Comp1210", 
      "percentage": "50.00", 
      "report": "null", 
      "description": "", 
      "subsidiary": "2", 
      "children": [{ 
       "id": "1004482749", 
       "parent": "1004482748", 
       "industry": "Akrotiri", 
       "industry_id": "industry25", 
       "name": "Comp12100", 
       "percentage": "100.00", 
       "report": "null", 
       "description": "", 
       "subsidiary": "1", 
       "children": [{ 
        "id": "1004482750", 
        "parent": "1004482749", 
        "industry": "Akrotiri", 
        "industry_id": "industry26", 
        "name": "Comp121000", 
        "percentage": "100.00", 
        "report": "null", 
        "description": "", 
        "subsidiary": "1", 
        "children": [{ 
         "id": "aa02639", 
         "parent": "1004482750", 
         "industry": "", 
         "industry_id": "0", 
         "name": "Sef 9", 
         "percentage": "9", 
         "report": "null", 
         "description": "", 
         "subsidiary": "0", 
         "children": [] 
        }] 
       }] 
      }] 
     }, { 
      "id": "", 
      "parent": "", 
      "industry": "", 
      "industry_id": "", 
      "name": "", 
      "percentage": "", 
      "report": "", 
      "description": "", 
      "subsidiary": "", 
      "children": "" 
     }] 
    }, { 
     "id": "1004482746", 
     "parent": "1004482744", 
     "industry": "Akrotiri", 
     "industry_id": "industry22", 
     "name": "Comp121", 
     "percentage": "30.00", 
     "report": "null", 
     "description": "", 
     "subsidiary": "1", 
     "children": [{ 
      "id": "", 
      "parent": "", 
      "industry": "", 
      "industry_id": "", 
      "name": "", 
      "percentage": "", 
      "report": "", 
      "description": "", 
      "subsidiary": "", 
      "children": "" 
     }] 
    }, { 
     "id": "1004482747", 
     "parent": "1004482744", 
     "industry": "Akrotiri", 
     "industry_id": "industry23", 
     "name": "Comp122", 
     "percentage": "30.00", 
     "report": "null", 
     "description": "", 
     "subsidiary": "1", 
     "children": [{ 
      "id": "aa02640", 
      "parent": "1004482747", 
      "industry": "", 
      "industry_id": "0", 
      "name": "Sef 10", 
      "percentage": "10", 
      "report": "null", 
      "description": "", 
      "subsidiary": "0", 
      "children": [] 
     }] 
    }] 
}] 
} 

在這個例子中我有一些元件,看起來像這樣:

"children": [{ 
      "id": "", 
      "parent": "", 
      "industry": "", 
      "industry_id": "", 
      "name": "", 
      "percentage": "", 
      "report": "", 
      "description": "", 
      "subsidiary": "", 
      "children": "" 
     } 

我想什麼才達到是從原始數組中消除這個「兒童」。 到目前爲止,我已經嘗試過這個功能,但它不工作的方式我想:我已經creatd這裏的jsfiddle

function deleteEmpty(data){ 
    if (data.children.length) 
    { 

     nr = data.children.length; 
     for (i=0;i<nr;i++) 
     { 
      // console.log(data.children[i]); 

      if (data.children[i]["id"] == ""){ 
       console.log(data.children[i]); 
       delete data.children[i]; 
      }     
       this.deleteEmpty(data.children[i]); 

     } 
    } 
    return data; 
} 

另外:http://jsfiddle.net/fP7NS/2/ 請幫助我或者給我一些提示與我是什麼做錯了。

+0

你只想刪除空白的孩子嗎? – 2013-03-17 09:35:40

+0

空白的兒童和那些在這個例子中的那些,其中來自「孩子」的鍵具有值「」。 – 2013-03-17 09:37:07

+0

JS小提琴中的對象'a'似乎有些問題。 – nhahtdh 2013-03-17 09:38:19

回答

1

首先(如有必要)將您的JSON字符串轉換爲對象。你的children屬性有時是一個數組,有時是一個空數組,有時是一個空字符串。這使得邏輯有點尷尬。我建議你寫一個函數來測試一個特定的孩子是否爲空。然後你可以這樣寫你的主循環:

function isEmpty(node) { 
    // logic for testing that a node is empty 
} 
function deleteEmptyChildren(node) { 
    // assumes node is not empty 
    if (Array.isArray(node.children)) { 
     node.children = node.children.filter(
      function(child) { 
       if (isEmpty(child)) { 
        return false; 
       } else { 
        deleteEmptyChildren(child); 
        return true; 
       } 
      } 
     ); 
    } 
}