1

I have this code. It checks if auctions[0] exists and if it does, it gets the value and sends it and continues to the next number. If not it will move on to the next number and do the same thing. And I need it to check if it exists until it reaches number 30

Are there any alternatives to this that is less bulky and messy?

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}`)
}
Julien Jacobs
  • 2,561
  • 1
  • 24
  • 34
  • Does this answer your question? [Loop through an array in JavaScript](https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript) – Dupinder Singh Aug 17 '20 at 11:28

2 Answers2

4

Use a for loop:

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}`)
     }
}
0

try something like this.

auctions.length this will return the lenght of array, and this will help you to iterate over all the elements.

This is the running code snippet.

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);
 }
}

And your full code will look like this:

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}`)
     }
}
Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61