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

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日

悬赏问题

  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 定制ai直播实时换脸软件
  • ¥100 栈回溯相关,模块加载后KiExceptionDispatch无法正常回溯了
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件