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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题