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日

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?