一尘不染

NodeJS计数数组中有多少个对象?

node.js

我如何计算数组中有多少个对象?

数组看起来像:

[ {id: 1}, {id: 2}, ...]

我假设如果是PHP,我可以使用count(),但是NodeJS / Javascript呢?

编辑:

阿斯

if (offer.items_to_receive.length > 0) { 
    console.log("items: " + offer.items_to_receive.length);
    for(var i = 0; i < offer.items_to_receive.length; i++) {
        usersInRound.push(offer.steamid_other);
    }
}

logData('Accepted trade offer from ' + offer.steamid_other + '. (tradeofferid: ' + offer.tradeofferid + ')\nWorth ' + offer.items_to_receive.length + ' tickets. ');

它怎么会读“价值X票”,而不能读另一部分?


阅读 217

收藏
2020-07-07

共1个答案

一尘不染

.length

var data = [{

    id: 1

}, {

    id: 2

}];



console.log(data.length);

更新 ,在您的编辑中,我看到

offer.items_to_receiveundefined,确保对象offer具有属性items_to_receive(应为数组);

2020-07-07