gpt4 book ai didi

javascript - 从数组检索数据无法正常工作

转载 作者:行者123 更新时间:2023-11-28 03:47:09 26 4
gpt4 key购买 nike

我有这个数组

[
{id: "1280-00-477-5893-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514575144592", name: "Cancelled -> Replaced by 1280005462575", price: "$324", imgsrc: "https://firebasestorage.googleapis.com/v0/b/honeyb…=media&token=3acd47c0-773a-48c1-8956-9a025769b91e", availableqty: "4", …}
{id: "1510-00-140-1627-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514546926962", name: "AIRPLANE UTILITY", price: "$890", imgsrc: "https://firebasestorage.googleapis.com/v0/b/honeyb…=media&token=01267792-8f7f-40a6-921b-699e4366e5cb", availableqty: "65", …}
]

我想使用每个对象的 id 并针对 firebase 数据库运行它以检索更多数据。

  $('.checkout').click(function(){

// is user authenticated

firebase.auth().onAuthStateChanged((user) => {
if (user) {
var userId = firebase.auth().currentUser.uid;
for (var i = 0; i < cartarry.length; i++) {
var id = cartarry[i].id;
var finalqty = cartarry[i].quantity;
var price = cartarry[i].price;
var itemref = Cataloguedatabase.ref('/Listings/' + id).once('value', function(snapshot) {
var listingdetail = snapshot.val();
// subtract ordered quantity from available quantity
var availableqty = parseInt(listingdetail.Ava_QTY);
var orderedqty = parseInt(finalqty);
var qtyleft = availableqty - orderedqty;
console.log("orderd qty ", orderedqty)
console.log("Available qty ", availableqty)
console.log("What is now left is ", qtyleft)
console.log("priceee is ", price)

})
}

} else {

window.location = "../html/auth.html"
}

})

})

该代码可以工作,但由于某种原因,它只打印出价格和orderedqty的最后一个数组值。例如,它不是打印 324 美元和 890 美元,而是打印两次 890 以及类似的数量。我做错了什么以及如何解决这个问题?

最佳答案

使用 let 声明变量,而不是 var

let id = cartarry[i].id;
let finalqty = cartarry[i].quantity;
let price = cartarry[i].price;

关于javascript - 从数组检索数据无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48359402/

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