Parsing javascript-jquery data formats

Source: Internet
Author: User
Through: data statistics methods, jQuery, urgent online, etc. A group of data is obtained through the console. log, the output format is as follows] [2] {code ...} question: How can I use jquery to parse this data structure and output the content at a specified position... try: I try to add [...

Through: data statistics methods, jQuery, urgent online, etc.
A group of data is obtained. After console. log, the output format is as follows:
] [2]

// The final data structure is // {// 'product key1': {// prod: 'product name1', // event: {// 'event type key1 ': {type: 'event type 1', count: event quantity}, // 'event type key2': {type: 'event type 2', count: event quantity} //}, // 'product key2': {// prod: 'product name2', // event: {// 'event type key3 ': {type: 'event type 3', count: event quantity}, // 'event type key4': {type: 'event type 4', count: event count }//}//},//... //}

Problem:
How can I use jquery to parse this data structure and output the content at a specified position...

Try:
I tried to add [''] After the result-although I knew it was definitely wrong, I tried it again. Yes, it was really wrong.
JSON. parse (result) is used. The output result also reports an error...
Solution

Reply content:

Through: data statistics methods, jQuery, urgent online, etc.
A group of data is obtained. After console. log, the output format is as follows:
] [2]

// The final data structure is // {// 'product key1': {// prod: 'product name1', // event: {// 'event type key1 ': {type: 'event type 1', count: event quantity}, // 'event type key2': {type: 'event type 2', count: event quantity} //}, // 'product key2': {// prod: 'product name2', // event: {// 'event type key3 ': {type: 'event type 3', count: event quantity}, // 'event type key4': {type: 'event type 4', count: event count }//}//},//... //}

Problem:
How can I use jquery to parse this data structure and output the content at a specified position...

Try:
I tried to add [''] After the result-although I knew it was definitely wrong, I tried it again. Yes, it was really wrong.
JSON. parse (result) is used. The output result also reports an error...
Solution

If you want your data to be like this

// [// Prod: 'product name1', // event: [// {type: 'event type 1', count: event quantity}, // {type: 'event type 2', count: event quantity} //] //}, // {// prod: 'product name 2', // event: [// {type: 'event type 3', count: event quantity}, // {type: 'event type 4', count: event quantity} //] //} //]

Then convert the data structure of the original result:

var result2=[];for(var key in result){    if(result.hasOwnProperty(key)){        result2.push(result[key]);    }}result2.forEach(function(item,index){    var events=[];    for(var eventKey in item['event']){        if(item['event'].hasOwnProperty(eventKey)){            events.push(item['event'][eventKey]);        }    }    item.events=events;});console.log(result2);

Q: Who wrote the previous data structure? Fan him ......

Do you want to bind an event to the row of the corresponding product Id on the page?

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.