Js Method for Traversing tree arrays with closures _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to use closures to traverse tree arrays in js. If you need a friend, you can refer to the following method when working on a company project, the parameters of the method are a menu array and a menu id. The format of the menu array is tree json, as shown below:

The Code is as follows:

[{"Id": 28, "text": "company information", "children ":[

{"Id": 1, "text": "Corporate Culture "},

{"Id": 2, "text": "Recruitment Plan "},

{"Id": 6, "text": "company news", "children ":[

{"Id": 47, "text": "industry news"}]},

{"Id": 11, "text": "Internal News", "children ":[

{"Id": 24, "text": "Administrative Information "},

{"Id": 27, "text": "high-level indicator"}]},

{"Id": 22, "text": "Contact Us "},

{"Id": 26, "text": "product display", "children ":[

{"Id": 32, "text": "Power Products "},

{"Id": 33, "text": "accessory Introduction"}]

}]

Now the menu id is 32. You need to find the corresponding item and return the corresponding menu name. The method is to traverse the array cyclically. When the item id is equal to the specified id, remove the menu name. If it is not equal to or greater than 0, check whether the current item has children. If children is not empty and the number is greater than 0, traverse the children. In this case, use the javascript closure, put the method that traverses children in an anonymous method, so that you can always recursion itself in an anonymous method. When an id with the same name is encountered, the loop jumps out, then return the menu name from the main method. The Code is as follows:

The Code is as follows:

Function getMenuName (menus, id ){
Var name = "";
For (var I = 0; I <menus. length; I ++ ){
If (menus [I]. id = id ){
Name = menus [I]. text;
Break;
}
Else {
(Function (){
Var m = arguments [0];
Var menuid = arguments [1];
For (var j = 0; j <m. length; j ++ ){
If (m [j]. id = menuid ){
Name = m [j]. text;
Break;
}
Else if m [j]. children! = Null & m [j]. children. length> 0 ){
Arguments. callee (m [j]. children, val); // recursive anonymous method
}
}
}) (Menus [I]. children, id );
}
}
Return name;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.