m0_55977331 2021-08-20 18:42 采纳率: 76.2%
浏览 32
已结题

这是有关一个过滤数据的问题。不会啊

后端返回来的数据 reviewerAssignNum 和 paperQuestionIdList
reviewerAssignNum: [{pageNo: null, limitSize: null, operatorId: 70, operatorName: "azheng", questionId: 15224, num: 1},…]
0: {pageNo: null, limitSize: null, operatorId: 70, operatorName: "azheng", questionId: 15224, num: 1}
1: {pageNo: null, limitSize: null, operatorId: 70, operatorName: "azheng", questionId: 15229, num: 3}
2: {pageNo: null, limitSize: null, operatorId: 70, operatorName: "azheng", questionId: 15232, num: 3}
3: {pageNo: null, limitSize: null, operatorId: 70, operatorName: "azheng", questionId: 15233, num: 3}
4: {pageNo: null, limitSize: null, operatorId: 72, operatorName: "张三", questionId: 15220, num: 3}
5: {pageNo: null, limitSize: null, operatorId: 72, operatorName: "张三", questionId: 15224, num: 1}
6: {pageNo: null, limitSize: null, operatorId: 72, operatorName: "张三", questionId: 15226, num: 3}
7: {pageNo: null, limitSize: null, operatorId: 72, operatorName: "张三", questionId: 15230, num: 3}
8: {pageNo: null, limitSize: null, operatorId: 72, operatorName: "张三", questionId: 15234, num: 3}
9: {pageNo: null, limitSize: null, operatorId: 73, operatorName: "TEST", questionId: 15219, num: 3}
10: {pageNo: null, limitSize: null, operatorId: 73, operatorName: "TEST", questionId: 15224, num: 1}
11: {pageNo: null, limitSize: null, operatorId: 73, operatorName: "TEST", questionId: 15228, num: 3}
]

paperQuestionIdList: [15224, 15226, 15228, 15229, 15230, 15231, 15232, 15234, 15219, 15233, 15220]

有这样一个数组,是后端返回回来的数据,我们要根据 paperQuestionIdList 中 数字 对应 reviewerAssignNum 中的 questionId, 转换成一个 这样的数据:(是个对象 key值是 questionId value 值 是 reviewerAssignNum 数组中 相同 的questionId 的 对象)
{
15219: [{…}]
15220: [{…}]
15224: (3) [{…}, {…}, {…}]
15226: [{…}]
15228: [{…}]
15229: [{…}]
15230: [{…}]
15231: [{…}]
15232: [{…}]
15233: [{…}]
15234: [{…}]
}.

  • 写回答

3条回答 默认 最新

  • 崽崽的谷雨 2021-08-20 19:44
    关注

    1.先生成一个对象
    2.循环遍历数据和遍历对象。然后push进去

    
    let reviewerAssignNum = [
            { pageNo: null, limitSize: null, operatorId: 70, operatorName: "azheng", questionId: 15224, num: 1 },
            { pageNo: null, limitSize: null, operatorId: 70, operatorName: "azheng", questionId: 15229, num: 3 },
            { pageNo: null, limitSize: null, operatorId: 70, operatorName: "azheng", questionId: 15232, num: 3 },
            { pageNo: null, limitSize: null, operatorId: 70, operatorName: "azheng", questionId: 15233, num: 3 },
            { pageNo: null, limitSize: null, operatorId: 72, operatorName: "张三", questionId: 15220, num: 3 },
            { pageNo: null, limitSize: null, operatorId: 72, operatorName: "张三", questionId: 15224, num: 1 },
            { pageNo: null, limitSize: null, operatorId: 72, operatorName: "张三", questionId: 15226, num: 3 },
            { pageNo: null, limitSize: null, operatorId: 72, operatorName: "张三", questionId: 15230, num: 3 },
            { pageNo: null, limitSize: null, operatorId: 72, operatorName: "张三", questionId: 15234, num: 3 },
            { pageNo: null, limitSize: null, operatorId: 73, operatorName: "TEST", questionId: 15219, num: 3 },
            { pageNo: null, limitSize: null, operatorId: 73, operatorName: "TEST", questionId: 15224, num: 1 },
            { pageNo: null, limitSize: null, operatorId: 73, operatorName: "TEST", questionId: 15228, num: 3 },
            { pageNo: null, limitSize: null, operatorId: 73, operatorName: "999", questionId: 15228, num: 3 }
        ]
    
        let paperQuestionIdList = [15224, 15226, 15228, 15229, 15230, 15231, 15232, 15234, 15219, 15233, 15220];
        console.log(fn(reviewerAssignNum, paperQuestionIdList))
        function fn(arr, keyArr) {
            if (arr.lenght == 0 || keyArr.lenght == 0) {
                return {};
            }
            let obj = {};
            keyArr.map((val) => {
                obj[val] = [];
            })
            console.log(obj)
            arr.map((item, index) => {
                for (let key in obj) {
                    if (item.questionId == key) {
                        obj[key].push(item);
                    }
                }
            })
            return obj;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 8月28日
  • 已采纳回答 8月20日
  • 创建了问题 8月20日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵