Home > Web Front-end > JS Tutorial > body text

How to process json data (code example)

不言
Release: 2018-09-17 15:49:29
Original
1623 people have browsed it

The content of this article is about how to process json data (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

If the name value is empty, it is classified as other. If the array length is greater than 15, the part exceeding 15 is classified as other.

adata = {"resolution":{"540x960":3,"1080x1920":4,"1080x2160":2,"720x1080":1},
                "brand":{"":10},
                "version":{"5.1.1":2,"7.0":2,"5.0.2":2,"7.1.1":2,"8.0.0":1,"4.4.4":1,"5.1":2,"7.1":2,"5.5.2":2,"":2,"8.0.1":1,"4.4":1,"5.6":2,"9.0":2,"6.0.2":2,"":2,"8.3":1,"4.7":1,"4.2":2,"9.6":1,"7.8.0":2},
                "errorreason":{"":8,"APK Crash":2}};
                
getConfigData: function(category) {
      /*如果name值为空归为其它,如果数组长度大于15,超出15的部分归为其它*/
      let adata = [];
      for (let i in category) {
        let a = 0, bdata = [], bbdata = [];
        for (let key in category[i]) {
          if (key == '') {
            a += category[i][key]
            bbdata.push({name: '其它', y: a});
          } else {
            bdata.push({name: key, y: category[i][key]});
          }
        }
        if (bdata.length > 15) {
          let b = 0;
          bdata.slice(0, -15).forEach(function(item) {
            b += item.y;
          })
          bbdata[0].y = b + bbdata[0].y;
          bdata.splice(14,bdata.length-1,bbdata[0])
          adata.push(bdata)
        }else{
          adata.push(bdata.concat(bbdata))
        }
      }
      return adata;
    },
    
    getConfigData(adata);
Copy after login

The above is the detailed content of How to process json data (code example). For more information, please follow other related articles on the PHP Chinese website!

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