Table of Contents
回复内容:
Home Backend Development PHP Tutorial javascript - jquery 数据格式解析

javascript - jquery 数据格式解析

Jun 06, 2016 pm 08:23 PM
html javascript jquery php

通过:关于数据统计方法,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的行绑定事件?

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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

HTML Table Layout

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles