NodeJS计算数组中有多少个对象? [英] NodeJS Count how many objects in array?
本文介绍了NodeJS计算数组中有多少个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何计算数组中有多少个对象?
How would I count how many objects there are inside an array?
数组看起来像:
[ {id: 1}, {id: 2}, ...]
我假设我可以使用count()如果它是PHP,但是NodeJS / Javascript怎么样?
I assume I could use count() if it was PHP, but what about 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票,而不是其他部分?
How come it can read the "worth X tickets", but not the other part?
推荐答案
使用 .length
var data = [{
id: 1
}, {
id: 2
}];
console.log(data.length);
更新,在您的编辑中我看到
Update, in your edit I see that
offer.items_to_receive
是 undefined
,确保对象 offer
具有属性 items_to_receive
(应为数组);
offer.items_to_receive
is undefined
, ensure that object offer
has property items_to_receive
(should be array);
这篇关于NodeJS计算数组中有多少个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文