Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

js 循环对象key 放入数组顺序被改变?

pastFineData是一个顺序对象,循环根据key添加对应数据,然后组成新的数组,可是看下面的图片对象的key 顺序已经和pastFineData对象排列的顺序变了,怎样能让key顺序不变啊。

pastFineData: {
                deductionDate: '',
                resourceCode: '',
                dept: '',
                name: '',
                deductionType: '',
                //扣款标准
                deductionStandard: '',
                //扣款次数
                deductionNum: null,
                // 扣款金额
                // deductionMoney: null,
                // 备注
                remark: '',
                operatorName: ''
            }
            `const arrKey = this.pastFineKey = Object.keys(this.pastFineData);`
            
 for (var j = 0; j < cellArr[i].length; j++) {
                    // 按照顺序赋值key
                    let key = arrKey[j];
                    if (key) {
                        // 对象赋值,有值赋值,无值赋值null
                        data[key] = cellArr[i][j] ? cellArr[i][j] : 'null';
                    }
                }

image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

对象的 key 是没有顺序的,你仔细想想,这个顺序是没有意义的。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...