dx15618816293
Colin听雨
采纳率50%
2021-03-01 12:45

前端js或后端java,帮助解析对象数组

求助:

前台现有数据:

{"学生":["小红","小明","小白"],"班级":["1班","2班"]}

现需要转换为数据:

[{"学生":"小红","班级":"1班"},{"学生":"小明","班级":"1班"},{"学生":"小白","班级":"1班"},
{"学生":"小红","班级":"2班"},{"学生":"小明","班级":"2班"},{"学生":"小白","班级":"2班"}]
有没有大佬可以帮助

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • vbhfdghff hweiyu00 1月前
    var data = {"学生":["小红","小明","小白"],"班级":["1班","2班"]};
    var result = [];
    for(var i = 0; i < data.班级.length; i++) {
        for(var j = 0; j < data.学生.length; j++) {
            result.push({"学生":data.学生[j], "班级":data.班级[i]});
        }
    }
    console.log(result);
    点赞 评论 复制链接分享
  • candy_tadecheng 小菜鸡她的城 1月前
        var stu_info = {
          "学生":["小红","小明","小白"],
          "班级":["1班","2班"]
        }
        var keys = Object.keys(stu_info)
        var stu_arr = Object.values(stu_info)
        var new_arr = []
        stu_arr[0].map(item1=>{
          stu_arr[1].map(item2=>{
            new_arr.push({
              [keys[0]]:item1,
              [keys[1]]:item2
            })
          })
        })
        console.log(new_arr);
    点赞 评论 复制链接分享
  • qq_38334525 默默地写代码 1月前
    <script>
        var data = {"学生":["小红","小明","小白"],"班级":["1班","2班"]};
        var new_arr = [];
        var students = data['学生'];
        var _class_ = data['班级'];
        students.map(function(item){
            _class_.map(function(item2){
                var new_obj = {};
                new_obj['学生'] = item;
                new_obj['班级'] = item2;
                new_arr.push(new_obj);
            })
            
        })
        console.log(new_arr);
    
        var new_arr2 = [];
        _class_.map(function(item){
            students.map(function(item2){
                var new_obj2 = {};
                new_obj2['学生'] = item2;
                new_obj2['班级'] = item;
                new_arr2.push(new_obj2);
            })
            
        })
        console.log(new_arr2);
    </script>
    点赞 评论 复制链接分享