计算数组中的对象?

新手上路,请多包涵

我有一个这样的数组:

 [{"this":"that","int":5},{"this":"that","int":5}]

如何使用 Javascript 计算数组中对象 ({}) 的数量?

提前致谢。

原文由 krmax44 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 300
2 个回答

尝试,

 var cnt = 0;
var arr = [5 , 3 , "not an object" , {"this":"that","int":5},{"this":"that","int":5}];

arr.forEach(function(itm){
 if(!itm.__proto__.__proto__){
  cnt++;
 }
});

console.log(cnt + "normal objects are there"); //2

原文由 Rajaprabhu Aravindasamy 发布,翻译遵循 CC BY-SA 3.0 许可协议

[{"this":"that","int":5},{"this":"that","int":5}].length; // 2

原文由 madox2 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题