m0_55977331 2022-05-23 09:44 采纳率: 76.2%
浏览 84
已结题

javascript转换数据结构

如何将 result 转换为 newResult 的数据结构, 根据 groupQuestionId的相同, 来添加index, 作为序号

```javascript
let result = [
      {avgScore: null,createTime: "2022-05-20 13:59:05",createUserId: 61622,groupQuestionId: 15615},
      {avgScore: null,createTime: "2022-05-20 13:59:05",createUserId: 61622,groupQuestionId: 15615},
      {avgScore: null,createTime: "2022-05-20 13:59:05",createUserId: 61622,groupQuestionId: 15615}, 
      {avgScore: null,createTime: "2022-05-20 13:59:05",createUserId: 61622,groupQuestionId: 15616},
      {avgScore: null,createTime: "2022-05-20 13:59:05",createUserId: 61622,groupQuestionId: 15617}
    ]

    let newResult = [
      {avgScore: null,createTime: "2022-05-20 13:59:05",createUserId: 61622,groupQuestionId: 15615, index: '1-1'},
      {avgScore: null,createTime: "2022-05-20 13:59:05",createUserId: 61622,groupQuestionId: 15615, index: '1-2'},
      {avgScore: null,createTime: "2022-05-20 13:59:05",createUserId: 61622,groupQuestionId: 15615, index: '1-3'}, 
      {avgScore: null,createTime: "2022-05-20 13:59:05",createUserId: 61622,groupQuestionId: 15616, index: '2'},
      {avgScore: null,createTime: "2022-05-20 13:59:05",createUserId: 61622,groupQuestionId: 15617, index: '3'}
    ]

```

  • 写回答

2条回答 默认 最新

  • web修理工 2022-05-23 10:17
    关注
    
    
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
        <script>
            // 如何将 result 转换为 newResult 的数据结构
    
    
            let result = [
                { avgScore: null, createTime: "2022-05-20 13:59:05", createUserId: 61622, groupQuestionId: 15615 },
                { avgScore: null, createTime: "2022-05-20 13:59:05", createUserId: 61622, groupQuestionId: 15615 },
                { avgScore: null, createTime: "2022-05-20 13:59:05", createUserId: 61622, groupQuestionId: 15615 },
                { avgScore: null, createTime: "2022-05-20 13:59:05", createUserId: 61622, groupQuestionId: 15616 },
                { avgScore: null, createTime: "2022-05-20 13:59:05", createUserId: 61622, groupQuestionId: 15617 }
            ]
    
            let new_arr = [], newResult = [];
            result.forEach((item, index) => {
                if (Array.isArray(new_arr) && new_arr.length > 0) {
                    let new_idnex = new_arr.findIndex(zitem => zitem.some(sitem => sitem.groupQuestionId == item.groupQuestionId))
                    if (new_idnex != -1) {
                        new_arr[new_idnex].push(item)
                    } else {
                        new_arr.push([item])
                    }
                } else {
                    new_arr.push([item])
                }
            })
    
            new_arr.forEach((item, index) => {
                if (Array.isArray(item) && item.length > 0) {
                    item.forEach((zitem, zindex, itemarr) => {
                        if (itemarr.length > 1) {
                            zitem.index = (index + 1) + '-' + (zindex + 1)
                            newResult.push(zitem)
                        } else {
                            zitem.index = (index + 1) + ''
                            newResult.push(zitem)
                        }
    
                    })
                }
            })
    
    
    
    
            console.log(JSON.stringify(newResult, null, 2))
    
    
            // let newResult = [
            //     { avgScore: null, createTime: "2022-05-20 13:59:05", createUserId: 61622, groupQuestionId: 15615, index: '1-1' },
            //     { avgScore: null, createTime: "2022-05-20 13:59:05", createUserId: 61622, groupQuestionId: 15615, index: '1-2' },
            //     { avgScore: null, createTime: "2022-05-20 13:59:05", createUserId: 61622, groupQuestionId: 15615, index: '1-3' },
            //     { avgScore: null, createTime: "2022-05-20 13:59:05", createUserId: 61622, groupQuestionId: 15616, index: '2' },
            //     { avgScore: null, createTime: "2022-05-20 13:59:05", createUserId: 61622, groupQuestionId: 15617, index: '3' }
            // ]
        </script>
    </body>
    
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月31日
  • 已采纳回答 5月23日
  • 修改了问题 5月23日
  • 创建了问题 5月23日

悬赏问题

  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件