我如何计算数组中有多少个对象?
数组看起来像:
[ {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票”,而不能读另一部分?
用 .length
.length
var data = [{ id: 1 }, { id: 2 }]; console.log(data.length);
更新 ,在您的编辑中,我看到
offer.items_to_receive是undefined,确保对象offer具有属性items_to_receive(应为数组);
offer.items_to_receive
undefined
offer
items_to_receive