gpt4 book ai didi

Javascript对象数组长度与对象数量不匹配

转载 作者:行者123 更新时间:2023-11-28 04:12:04 24 4
gpt4 key购买 nike

有一个我正在创建并将对象推送到的 Javascript 数组:

checkedAPBoxes.each( function() {
let APTempObj = {};
let apSequence = $(this).data('sequence');
let arDropdownSequenceValue = $('#billingRelaseSelectTiedAR' + $(this).data('sequence')).val();

APTempObj.apSequenceToRelease = apSequence;
APTempObj.relatedARSequence = arDropdownSequenceValue;
APArray.push(APTempObj);

//check if current invoice is comcheck
const url = '/cfc/shipments/ap/ShipmentAPAPI.cfc';
const data = {
method: 'isOriginalComcheckInvoice',
shipmentID: shipmentID,
sequence: apSequence,
returnformat: 'JSON'
};

Edge.Utility.Ajax.get(url, data)
.then(function(result) {
if (result == true){
//add related comcheck invoices to batch
const comcheckURL = '/cfc/shipments/ap/ShipmentAPAPI.cfc';
const comcheckData = {
method: 'getRelatedComcheckInvoices',
shipmentID: shipmentID,
sequence: apSequence,
returnformat: 'json'
};

Edge.Utility.Ajax.get(comcheckURL, comcheckData)
.then(function(resultAPs) {
for (var i = 0; i < resultAPs.length; i++) {
let relatedAPTempObj = {};
relatedAPTempObj.apSequenceToRelease = parseInt(resultAPs[i]);
relatedAPTempObj.relatedARSequence = arDropdownSequenceValue;
APArray.push(relatedAPTempObj);
}
}).catch(function(xhr) {
console.error(xhr);
$uiButton.eui('button', 'complete');
$uiButton.show();
});
}
}).catch(function(xhr) {
console.error(xhr);
$uiButton.eui('button', 'complete');
$uiButton.show();
});
});

但是记录数组显示长度为 5,但其中只有 3 个对象:

enter image description here

基于长度循环数组仅打印前三个元素,即使长度显示为 5。

        for (var i = 0; i < APArray.length; i++) {
console.log('typeof: ' + typeof(APArray[i]));
console.log(APArray[i]);
}

最佳答案

ajax.then(callback) 和 ajax.done(callback) 之间有区别。 “then”函数每次收到数据时都会返回一个新的 Promise。这就是为什么当收到第一批输出(即 3 个对象)时,它会执行回调函数并输出 3 个对象,并 promise 稍后会再接收 2 个对象。

所以解决方法是使用“done”函数而不是“then”。在这种情况下,如果完全成功,只会有一次回调。在这种情况下,您将收到 5 个元素。请参阅下面更新的代码。

checkedAPBoxes.each( function() {
let APTempObj = {};
let apSequence = $(this).data('sequence');
let arDropdownSequenceValue = $('#billingRelaseSelectTiedAR' + $(this).data('sequence')).val();

APTempObj.apSequenceToRelease = apSequence;
APTempObj.relatedARSequence = arDropdownSequenceValue;
APArray.push(APTempObj);

//check if current invoice is comcheck
const url = '/cfc/shipments/ap/ShipmentAPAPI.cfc';
const data = {
method: 'isOriginalComcheckInvoice',
shipmentID: shipmentID,
sequence: apSequence,
returnformat: 'JSON'
};

Edge.Utility.Ajax.get(url, data)
.done(function(result) {
if (result == true){
//add related comcheck invoices to batch
const comcheckURL = '/cfc/shipments/ap/ShipmentAPAPI.cfc';
const comcheckData = {
method: 'getRelatedComcheckInvoices',
shipmentID: shipmentID,
sequence: apSequence,
returnformat: 'json'
};

Edge.Utility.Ajax.get(comcheckURL, comcheckData)
.done(function(resultAPs) {
for (var i = 0; i < resultAPs.length; i++) {
let relatedAPTempObj = {};
relatedAPTempObj.apSequenceToRelease = parseInt(resultAPs[i]);
relatedAPTempObj.relatedARSequence = arDropdownSequenceValue;
APArray.push(relatedAPTempObj);
}
}).catch(function(xhr) {
console.error(xhr);
$uiButton.eui('button', 'complete');
$uiButton.show();
});
}
}).catch(function(xhr) {
console.error(xhr);
$uiButton.eui('button', 'complete');
$uiButton.show();

关于Javascript对象数组长度与对象数量不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46182552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com