吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1659|回复: 15
收起左侧

[求助] JS数组格式转换的问题

[复制链接]
cqwcns 发表于 2021-4-12 21:00
服务器返回大量数据,格式大概是这样的。

[JavaScript] 纯文本查看 复制代码
{
count: 8,
id: {responsibleRegion: "市公司", responsibleGrid: "生产室", responsibleName: "唐生", workType: "核查资源", isComplete: false}
},{
count: 7,
id: {responsibleRegion: "茂南 ", responsibleGrid: "城东", responsibleName: "李生", workType: "弱光整治", isComplete: false}
},{
count: 16,
id: {responsibleRegion: "高州", responsibleGrid: "城西", responsibleName: "梁生", workType: "弱光整治", isComplete: true}
}


先说明一下数据,数据的层级关系是这样的:workType→responsibleRegion→responsibleGrid→responsibleName,即工作类型→区域→网格→个人。
count是数量,isComplete通过true或false确定是isComplete或notComplete。
我希望统计出各层级的情况,通过item承载下一级,如[{name: "核查资源",isComplete: 88,notComplete: 66,item:[...],...]。
将数据转换成大概这样:

[Asm] 纯文本查看 复制代码
[
{name: "核查资源",isComplete: 88,notComplete: 66,item: [{name: "茂南",isComplete: 38,notComplete: 36,item: [{name: "城东",isComplete: 28,notComplete: 26,item:[name:"李生"isComplete: 8,notComplete: 6]}]}]},
...
]


我知道要for循环,但情况比较复杂,搞了半天没想明白怎么搞,求指点。



免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
FY-1573 + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

a147888123 发表于 2021-4-12 21:07
json数据啊,用JSON来解
 楼主| cqwcns 发表于 2021-4-12 21:18
艹,这嵌套太复杂了,写不下去了。
数据结构大概是这样,首次写入,第二次就不知道怎么写了。请指教。

[JavaScript] 纯文本查看 复制代码
 //循环统计数据
        const DATA_ALL = THIS_RES.all
        var count_All = []
        var listType = []
        for (let i = 0; i < DATA_ALL.length; i++) {
          if (!listType.includes(DATA_ALL[i]._id.workType)) {
            listType.push(DATA_ALL[i]._id.workType)

            let isComplete = 0
            let notComplete = 0
            let allNum = DATA_ALL[i].count

            if (DATA_ALL[i]._id.isComplete) {
              isComplete = DATA_ALL[i].count
            } else {
              isComplete = DATA_ALL[i].count
            }

            count_All[listType.indexOf(DATA_ALL[i]._id.workType)] = {
              name: DATA_ALL[i]._id.workType,
              isComplete,
              notComplete,
              allNum,
              item: [{
                name: DATA_ALL[i]._id.responsibleRegion,
                isComplete,
                notComplete,
                allNum,
                item: [{
                  name: DATA_ALL[i]._id.responsibleGrid,
                  isComplete,
                  notComplete,
                  allNum,
                  item: [{
                    name: DATA_ALL[i]._id.responsibleName,
                    isComplete,
                    notComplete,
                    allNum
                  }]
                }]
              }]
            }


          }

          // console.log(DATA_ALL[i]._id.workType)
          // console.log(DATA_ALL[i]._id.responsibleRegion)
          // console.log(DATA_ALL[i]._id.responsibleGrid)
          // console.log(DATA_ALL[i]._id.responsibleName)
          // console.log(DATA_ALL[i].count)
        }
alterempty 发表于 2021-4-12 21:20
XiaoXin10 发表于 2021-4-12 21:39
建议按照 工作类型→区域→网格→个人这个层级关系,最外层一个大的对象存储,里面每层一个对象存储下一层的数据依次嵌套。
 楼主| cqwcns 发表于 2021-4-12 22:16
alterempty 发表于 2021-4-12 21:20
搜索一下 js for...in

for in我了解,但还是没想到怎么实现,给个思路,谢谢。
墨墨殿下 发表于 2021-4-12 23:28
map filter reduce foreach + 箭头函数
一天 发表于 2021-4-13 00:45
这不对啊,数据源有问题,这不是json,除非你把key提前定义好了
sail2000 发表于 2021-4-13 10:27
来源是三条 json,拼一下格式化:
[JavaScript] 纯文本查看 复制代码
{
    "source": [
        {
            "count": 8,
            "id": {
                "responsibleRegion": "市公司",
                "responsibleGrid": "生产室",
                "responsibleName": "唐生",
                "workType": "核查资源",
                "isComplete": false
            }
        },
        {
            "count": 7,
            "id": {
                "responsibleRegion": "茂南 ",
                "responsibleGrid": "城东",
                "responsibleName": "李生",
                "workType": "弱光整治",
                "isComplete": false
            }
        },
        {
            "count": 16,
            "id": {
                "responsibleRegion": "高州",
                "responsibleGrid": "城西",
                "responsibleName": "梁生",
                "workType": "弱光整治",
                "isComplete": true
            }
        }
    ]
}

你要的结果拼一下格式化:
[JavaScript] 纯文本查看 复制代码
{
    "result": [
        {
            "name": "核查资源",
            "isComplete": 88,
            "notComplete": 66,
            "item": [
                {
                    "name": "茂南",
                    "isComplete": 38,
                    "notComplete": 36,
                    "item": [
                        {
                            "name": "城东",
                            "isComplete": 28,
                            "notComplete": 26,
                            "item": [
                                {
                                    "name": "李生",
                                    "isComplete": 8,
                                    "notComplete": 6
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

就是解析Json取节点再组织json输出的事情
 楼主| cqwcns 发表于 2021-4-13 10:49
sail2000 发表于 2021-4-13 10:27
来源是三条 json,拼一下格式化:
[mw_shl_code=javascript,true]{
    "source": [

JS 实现哦,有JS代码吗?谢谢
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 05:22

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表