javascript - jquery 数据格式解析

WBOY
Release: 2016-06-06 20:23:55
Original
1330 people have browsed it

通过:关于数据统计方法,jQuery,急在线等。
获取了一组数据,经过console.log后,输出格式如下
javascript - jquery 数据格式解析][2]

<code>//最后的数据结构为
//{
//  '产品key1':{
//    prod:'产品名1',
//    event:{
//        ‘事件类型Key1’:{type:'事件类型1',count:事件数量},
//        ‘事件类型Key2’:{type:'事件类型2',count:事件数量}
//        }
//   },
//   '产品key2':{
//    prod:'产品名2',
//    event:{
//        ‘事件类型Key3’:{type:'事件类型3',count:事件数量},
//        ‘事件类型Key4’:{type:'事件类型4',count:事件数量}
//        }
//   },
//    ... 
// }</code>
Copy after login
Copy after login

问题:
请问这种数据结构,我应该如何用jquery解析并能够输出指定位置内容...

尝试:
我尝试在result后加['']- -虽然知道肯定是错的,但是还是试了下,没错真的是错的。
然后通过JSON.parse(result); 输出的结果也报错...
求解

回复内容:

通过:关于数据统计方法,jQuery,急在线等。
获取了一组数据,经过console.log后,输出格式如下
javascript - jquery 数据格式解析][2]

<code>//最后的数据结构为
//{
//  '产品key1':{
//    prod:'产品名1',
//    event:{
//        ‘事件类型Key1’:{type:'事件类型1',count:事件数量},
//        ‘事件类型Key2’:{type:'事件类型2',count:事件数量}
//        }
//   },
//   '产品key2':{
//    prod:'产品名2',
//    event:{
//        ‘事件类型Key3’:{type:'事件类型3',count:事件数量},
//        ‘事件类型Key4’:{type:'事件类型4',count:事件数量}
//        }
//   },
//    ... 
// }</code>
Copy after login
Copy after login

问题:
请问这种数据结构,我应该如何用jquery解析并能够输出指定位置内容...

尝试:
我尝试在result后加['']- -虽然知道肯定是错的,但是还是试了下,没错真的是错的。
然后通过JSON.parse(result); 输出的结果也报错...
求解

如果你希望你的数据结果是这样的

<code>//  [
//    prod:'产品名1',
//    event:[
//        {type:'事件类型1',count:事件数量},
//        {type:'事件类型2',count:事件数量}
//        ]
//   },
//    {
//    prod:'产品名2',
//    event:[
//        {type:'事件类型3',count:事件数量},
//        {type:'事件类型4',count:事件数量}
//        ]
//    }
// ]</code>
Copy after login

那么把原来的result的数据结构转换下:

<code>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);</code>
Copy after login

javascript - jquery 数据格式解析

就问一句:之前的数据结构谁写的?扇他……

你是想为页面上对应产品Id的行绑定事件?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!