Implementing a JSON array of multiple key values in JavaScript

Source: Internet
Author: User
Tags json

① first uses C # in the background to get the menu data in the database, to generate a JSON array containing the menu data items (for some reason not using the order by in SQL), as follows:

View Sourceprint?var menudata1=[

{"OrderID": -10, "text": "Home", "title": "Personal website home page", "url": "Default.aspx?" teacherid=153 "," visible ": true," target ":" _self "},

{" OrderID ": Ten," text ":" Personal News "," title ":" Personal News "," url ":" News.aspx? teacherid=153 "," visible ": true," target ":" _self "},

{" OrderID ": 5," text ":" Publish Paper "," title ":" Publish a paper "," url ":" Paper.aspx? teacherid=153 "," visible ": true," target ":" _self "},

{" OrderID ": 2," text ":" Publication Monograph "," title ":" Publication monograph "," url ":" Monograph.aspx? teacherid=153 "," visible ": true," target ":" _self "},

{" OrderID ": 7," text ":" Participating in Project "," title ":" Participating in Project "," url ":" Project.aspx? teacherid=153 "," visible ": true," target ":" _self "},

{" OrderID ": 8," text ":" Personal Honor "," title ":" Personal Honor "," url ":" Prize.aspx? teacherid=153 "," visible ": true," target ":" _self "},

{" OrderID ": 1," text ":" Invention Patent "," title ":" Invention Patent "," url ":" Patent.aspx? teacherid=153 "," visible ": true," target ":" _self "},

{" OrderID ": 0," text ":" File Share "," title ":" File Share "," url ":" Fileshare.aspx? teacherid=153 "," visible ": true," target ":" _self "}

];

② you need to sort the array in OrderID ascending order first, note that the sort method overwrites the original array

View Sourceprint?menudata1.sort (function (A, b) {return a.orderid > B.orderid 1:-1;}); /

After the ③ has been sorted, the result is:

View sourceprint?menudata1=[

{"OrderID": -10, "text": "Home", "title": "Personal site Home page", "url": "Default.aspx?" teacherid=153 "," visible ": true," target ":" _self "},

{" OrderID ": 0," text ":" File Share "," title ":" File Share "," url ":" Fileshare.aspx? teacherid=153 "," visible ": true," target ":" _self "},

{" OrderID ": 1," text ":" Invention Patent "," title ":" Invention Patent "," url ":" Patent.aspx? teacherid=153 "," visible ": true," target ":" _self "},

{" OrderID ": 2," text ":" Publication Monograph "," title ":" Publication monograph "," url ":" Monograph.aspx? teacherid=153 "," visible ": true," target ":" _self "},

{" OrderID ": 5," text ":" Publish Paper "," title ":" Publish a paper "," url ":" Paper.aspx? teacherid=153 "," visible ": true," target ":" _self "},

{" OrderID ": 7," text ":" Participating in Project "," title ":" Participating in Project "," url ":" Project.aspx? teacherid=153 "," visible ": true," target ":" _self "},

{" OrderID ": 8," text ":" Personal Honor "," title ":" Personal Honor "," url ":" Prize.aspx? teacherid=153 "," visible ": true," target ":" _self "},

{" OrderID ": Ten," text ":" Personal News "," title ":" Personal News "," url ":" News.aspx? teacherid=153 "," visible ": true," target ":" _self "}

];
 

④ then displays the Visual menu item based on the Menudata1[i].visible property (where String.Format see Implementing a string in JavaScript like C #. Features of format):

The Visual menu item is then displayed based on the Menudata1[i].visible property (where String.Format see "Implementing a string in JavaScript like C #." Features of format):

var menustring = "<ul><li class=" Menudiv "></li>";
for (Var i=0;i<menudata1.length;i++) {
if (menudata1[i].visible) {
Menustring + + String.Format ("<li><a title=" {0} "class=" Menua "onfocus=" This.blur () "href=" {1} "target=" {2} " >{3}</a></li><li class= "Menudiv" ></li> ", Menudata1[i].title, Menudata1[i].url, Menudata1[i].target, Menudata1[i].text);
}
}
menustring = "</ul>";
$ (menucontainer). InnerHTML = menustring;

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.