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日

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改