首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何一次检查多个数组?

如何一次检查多个数组?
EN

Stack Overflow用户
提问于 2020-08-17 17:47:20
回答 2查看 73关注 0票数 1

我有这个代码。它检查是否存在拍卖,如果存在,则获取值并将其发送并继续到下一个数字。如果不是,它将移动到下一个数字,并执行相同的操作。我需要它来检查它是否存在,直到它达到30号

有没有比这更简单、更整洁的替代方案呢?

代码语言:javascript
复制
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}`)
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-17 17:49:27

使用for循环:

代码语言:javascript
复制
for(let i = 0; i <= 30; i++) {
     if (ahValue.auctions[i]){
        var itemName = ahValue.auctions[i].item_name
        var itemLore = ahValue.auctions[i].item_lore
        var itemTier = ahValue.auctions[i].tier
        var itemSeller = ahValue.auctions[i].auctioneer
        var itemBids = ahValue.auctions[i].bids.length
            

        console.log(`${itemName}${itemLore}${itemTier}${itemSeller}${itemBids}`)
     }
}
票数 4
EN

Stack Overflow用户

发布于 2020-08-17 17:59:25

试试这样的东西。

auctions.length这将返回数组的长度,这将帮助你遍历所有元素。

这是正在运行的代码片段。

代码语言:javascript
复制
var auctions = [];
auctions[0] = { "item_name" : "item_name1",  "item_lore" : "item_lore1", "tier" : "tier1",  "auctioneer" : "auctioneer1" , "bids":"bids1"};
auctions[1] = { "item_name" : "item_name2",  "item_lore" : "item_lore2", "tier" : "tier2",  "auctioneer" : "auctioneer2" , "bids":"bids2"};


for(var i = 0; i < auctions.length; i++){
 if(auctions[i]){
          console.log(auctions[i].item_name);
 }
}

您的完整代码将如下所示:

代码语言:javascript
复制
for(let i = 0; i <= ahValue.auctions.length; i++) {
     if (ahValue.auctions[i]){
        var itemName = ahValue.auctions[i].item_name
        var itemLore = ahValue.auctions[i].item_lore
        var itemTier = ahValue.auctions[i].tier
        var itemSeller = ahValue.auctions[i].auctioneer
        var itemBids = ahValue.auctions[i].bids.length
            

        console.log(`${itemName}${itemLore}${itemTier}${itemSeller}${itemBids}`)
     }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63448388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档