JSON, the loop case of an array

Source: Internet
Author: User

Most of the small partners will be working with JSON and arrays, case up

var data = {

' 2018 ': [

{

' title ': ' Case One ',

' Name ': ' Fanny1 '

},

    

{

' title ': ' Case One ',

' Name ': ' Fanny2 '

}

],

' 2017 ': [

{

' title ': ' Case two ',

' Name ': ' Fanny1 '

},

    

{

' title ': ' Case two ',

' Name ': ' Fanny2 '

}

]

}

Most of the data that is normally received from the background is in this form

1.json loop inside: for (var key in data) {}

there is a way to get the key value directly Object.keys (data)

If you want to get a separate key value Object.keys (data) [0]

2. There are many kinds of loops in an array, for example a

var arraydata = Data[key];

Arraydata.map ((Item,index) =>{

Console.log (item)//To get every data in the array

})

3. If your outer layer needs this data, we'll stitch it up

var shtml= "";

for (var key in data) {

var reportdata = Data[key];

  SHTML1 + = ' <li> ';

SHTML1 + = ' <div class= ' menu_title ' ><span> ' + key + ' year </span></div> ';

SHTML1 + = ' <ul class= ' menu_content ' > ';

  ReportData. Map (Item,index) =>{
SHTML1 + = ' <li attr-href= ' +item.title+ ' attr-img= ' +item.name+ ' > ' +item.title+ ' </li> ';
}

SHTML1 + = ' </ul></li> ';

}

And then render it in

Document.queryselector (". Main"). AppendChild (SHTML1);

* Attention Issues

The key value in this case is a number, the default is ascending order, you need to arrange your own

JSON, the loop case of an array

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.