Search code examples
javascriptnode.jssteamsteambot

Steam trade-offer-manager get items info shorten


I'm trying to build a new project. It's going to be a tradebot for a website, now to store my received items into my database i whould like some info send with each item (being the name , asseid , tradeid,...). The following code works.

offers.on('receivedOfferChanged', function (offer, oldState) {
logger.info(offer.partner.getSteam3RenderedID() + " Offer #" + offer.id + " changed: " + TradeOfferManager.getStateName(oldState) + " -> " + TradeOfferManager.getStateName(offer.state));

// Alert us when we accept an offer
if (offer.state == TradeOfferManager.ETradeOfferState.Accepted) {
    offer.getReceivedItems(function (err, items) {
        if (err) {
            logger.error("Couldn't get received items: " + err);
        } else {
            var names = items.map(function(item) {
                return item.name;
            });
            var assetids = items.map(function(item) { 
                return item.assetid; 
            });
            // Log a comma-separated list of items received
            logger.info("Received: " + names + " " + assetids.join(', '));
        }
    });
}
});`

But the thing is, is there any way to shorten the following code :

var names = items.map(function(item) {
            return item.name;
        });
        var assetids = items.map(function(item) { 
            return item.assetid; 
        });

So it gets the item name , assetid, ... out of the array and stores them in sperate variables ?


Solution

  • You can use push() method to add values into both arrays in a single loop. Try:

    var names = [],
        assetids = [];
    
    items.forEach(function(item) {
        assetids.push(item.assetid); 
        names.push(item.name); 
    });