自在猫先生 2022-09-01 17:52 采纳率: 62.9%
浏览 50
已结题

JavaScript 前端问题 for循环数据会覆盖,每次取都是最后一行

问题遇到的现象和发生背景

我循环对象里面的数据,对象里面有两行数据,我每次循环出来取的都是最后一行,而且push出来的值还多了。

问题相关代码,请勿粘贴截图
 //读取选中行的数据
        if(chk!=undefined && chk.checked){
           //一个查询
            var param = getSearchParams();
            for (var i = 0; i <param.length; i++) {
                //item
                var ZYL02 = param[i].ZYL02;
                var ZYL01 = param[i].ZYL01;
                var ZID = param[i].ZID;
                var DATUV = "20220419";
                var WERKS = param[i].WERKS;
                var AETXT = param[i].AETXT;
                var AENNR = param[i].AENNR;
                //items
                var ZWYBS = param[i].ZWYBS;
                var ZMESVIN = param[i].ZMESVIN;
                var ZMESDATE = param[i].ZMESDATE;
                var IDNRK = param[i].IDNRK;
                var ZYL04 = param[i].ZYL04;
                var SORTF = "X";
                var ZYL06 = param[i].ZYL06;
                var ZCZZT = param[i].ZCZZT;
                var AUSCH = "";
                var ZYL05 = param[i].ZYL05;
                var ZYL03 = param[i].ZYL03;
                var MATNR = param[i].MATNR;
                // Items 
                item_list.ZYL02 = ZYL02;
                item_list.ZYL01 = ZYL01;
                item_list.ZID = ZID;
                item_list.DATUV = DATUV;
                item_list.WERKS = WERKS;
                item_list.AETXT = AETXT;
                item_list.AENNR = AENNR;
                item_list.ZYL03 = ZYL03;
                item_list.MATNR = MATNR;
                //item_list.ITEM = items_list;
                items_list.ZWYBS = ZWYBS;
                items_list.ZMESVIN = ZMESVIN;
                items_list.ZMESDATE = ZMESDATE;
                items_list.IDNRK = IDNRK;
                items_list.ZYL04 = ZYL04;
                items_list.SORTF = SORTF;
                items_list.ZYL06 = ZYL06;
                items_list.ZCZZT = ZCZZT;
                items_list.AUSCH = AUSCH;
                items_list.ZYL05 = ZYL05;
                //创建最外层响应头
                req.REQUEST = {};
                //赋值
                data.HEAD = head;
                data.DATA = REQUEST;
                REQUEST.ITEM = item;
                main.ITEMS = items;
                req.REQUEST = data;
                //追加
                item_list.ITEMS = items;
                item.push(item_list);
                items.push(items_list);
            }
            var JsonData = JSON.stringify(req);

运行结果及报错内容
得到的json字符串数据
{
    "REQUEST": {
        "HEAD": {
            "ACCOUNT": "ESB_TEST_MES", 
            "PASSWORD": "@esb", 
            "CONSUMER": "SAP", 
            "COUNT": "1", 
            "TRANSACTIONID": "", 
            "SRVLEVEL": "1", 
            "COMPANY": "BAW"
        }, 
        "DATA": {
            "ITEM": [
                {
                    "ZYL02": "", 
                    "ZYL01": "", 
                    "ZID": "LL0125001", 
                    "DATUV": "20220419", 
                    "WERKS": "1081", 
                    "AETXT": "VIN", 
                    "AENNR": "MCN00000041", 
                    "ZYL03": "", 
                    "MATNR": "LL0125", 
                    "ITEMS": [
                        {
                            "ZWYBS": "0PtNBcLR5dHOAA", 
                            "ZMESVIN": "SVU20220711LEDAAA", 
                            "ZMESDATE": "2022-09-01", 
                            "IDNRK": "000000010021000697", 
                            "ZYL04": "", 
                            "SORTF": "X", 
                            "ZYL06": "", 
                            "ZCZZT": "U", 
                            "AUSCH": "", 
                            "ZYL05": ""
                        }, 
                        {
                            "ZWYBS": "Fi08398e93ddc", 
                            "ZMESVIN": "SVU20220711LED2222", 
                            "ZMESDATE": "2022-09-01", 
                            "IDNRK": "000000010021000697", 
                            "ZYL04": "", 
                            "SORTF": "X", 
                            "ZYL06": "", 
                            "ZCZZT": "U", 
                            "AUSCH": "", 
                            "ZYL05": ""
                        }
                    ]
                }, 
                {
                    "ZYL02": "", 
                    "ZYL01": "", 
                    "ZID": "LL0125001", 
                    "DATUV": "20220419", 
                    "WERKS": "1081", 
                    "AETXT": "VIN", 
                    "AENNR": "MCN00000041", 
                    "ZYL03": "", 
                    "MATNR": "LL0125", 
                    "ITEMS": [
                        {
                            "ZWYBS": "0PtNBcLR5dHOAA", 
                            "ZMESVIN": "SVU20220711LEDAAA", 
                            "ZMESDATE": "2022-09-01", 
                            "IDNRK": "000000010021000697", 
                            "ZYL04": "", 
                            "SORTF": "X", 
                            "ZYL06": "", 
                            "ZCZZT": "U", 
                            "AUSCH": "", 
                            "ZYL05": ""
                        }, 
                        {
                            "ZWYBS": "0PtNBcLR5dHOAA", 
                            "ZMESVIN": "SVU20220711LEDAAA", 
                            "ZMESDATE": "2022-09-01", 
                            "IDNRK": "000000010021000697", 
                            "ZYL04": "", 
                            "SORTF": "X", 
                            "ZYL06": "", 
                            "ZCZZT": "U", 
                            "AUSCH": "", 
                            "ZYL05": ""
                        }
                    ]
                }
            ]
        }
    }
}
实际上得到的正确json字符串如下:
{
    "REQUEST": {
        "HEAD": {
            "ACCOUNT": "ESB_TEST_MES", 
            "PASSWORD": "@esb", 
            "CONSUMER": "SAP", 
            "COUNT": "1", 
            "TRANSACTIONID": "", 
            "SRVLEVEL": "1", 
            "COMPANY": "BAW"
        }, 
        "DATA": {
            "ITEM": [
                {
                    "ZYL02": "", 
                    "ZYL01": "", 
                    "ZID": "LL0125001", 
                    "DATUV": "20220419", 
                    "WERKS": "1081", 
                    "AETXT": "VIN", 
                    "AENNR": "MCN00000041", 
                    "ZYL03": "", 
                    "MATNR": "LL0125", 
                    "ITEMS": [
                        {
                            "ZWYBS": "0PtNBcLR5dHOAA", 
                            "ZMESVIN": "SVU20220711LEDAAA", 
                            "ZMESDATE": "2022-09-01", 
                            "IDNRK": "000000010021000697", 
                            "ZYL04": "", 
                            "SORTF": "X", 
                            "ZYL06": "", 
                            "ZCZZT": "U", 
                            "AUSCH": "", 
                            "ZYL05": ""
                        }, 
                        {
                            "ZWYBS": "Fi08398e93ddc", 
                            "ZMESVIN": "SVU20220711LED2222", 
                            "ZMESDATE": "2022-09-01", 
                            "IDNRK": "000000010021000697", 
                            "ZYL04": "", 
                            "SORTF": "X", 
                            "ZYL06": "", 
                            "ZCZZT": "U", 
                            "AUSCH": "", 
                            "ZYL05": ""
                        }
                    ]
                }, 
            
            ]
        }
    }
}

img

我的解答思路和尝试过的方法

我尝试过使用网上说的建立一个空对象去实现,但是达不到想到的效果,我都json格式会被打乱。

我想要达到的结果

把两个对象里面的值都追加进对象和数组里面。

  • 写回答

3条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-01 18:05
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月13日
  • 已采纳回答 9月5日
  • 创建了问题 9月1日

悬赏问题

  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败