首页 >web前端 >js教程 >json数据如何进行处理(代码实例)

json数据如何进行处理(代码实例)

不言
不言原创
2018-09-17 15:49:291629浏览

本篇文章给大家带来的内容是关于json数据如何进行处理(代码实例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

如果name值为空归为其它,如果数组长度大于15,超出15的部分归为其它

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);

以上是json数据如何进行处理(代码实例)的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn