【发布时间】:2020-08-17 19:51:28
【问题描述】:
我有这个代码。它检查auctions[0] 是否存在,如果存在,它会获取该值并将其发送并继续到下一个数字。如果不是,它会移动到下一个数字并做同样的事情。我需要它来检查它是否存在,直到它达到第 30 号
有没有什么替代品可以减少体积和杂乱?
if (ahValue.auctions[0]){
var itemName = ahValue.auctions[0].item_name
var itemLore = ahValue.auctions[0].item_lore
var itemTier = ahValue.auctions[0].tier
var itemSeller = ahValue.auctions[0].auctioneer
var itemBids = ahValue.auctions[0].bids.length
console.log(`${itemName}${itemLore}${itemTier}${itemSeller}${itemBids}`)
}
if (ahValue.auctions[1]){
var itemName = ahValue.auctions[1].item_name
var itemLore = ahValue.auctions[1].item_lore
var itemTier = ahValue.auctions[1].tier
var itemSeller = ahValue.auctions[1].auctioneer
var itemBids = ahValue.auctions[1].bids.length
console.log(`${itemName}${itemLore}${itemTier}${itemSeller}${itemBids}`)
}
//copy and paste until it reaches 30
if (ahValue.auctions[30]){
var itemName = ahValue.auctions[30].item_name
var itemLore = ahValue.auctions[30].item_lore
var itemTier = ahValue.auctions[30].tier
var itemSeller = ahValue.auctions[30].auctioneer
var itemBids = ahValue.auctions[30].bids.length
console.log(`${itemName}${itemLore}${itemTier}${itemSeller}${itemBids}`)
}
【问题讨论】:
-
这能回答你的问题吗? Loop through an array in JavaScript
标签: javascript json