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

编程入门 行业动态 更新时间:2024-10-28 06:32:25
本文介绍了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计算数组中有多少个对象?

本文发布于:2023-10-20 13:08:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1511058.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中有   数组   多少个   对象   NodeJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!