简体   繁体   English

Angular 4 MEAN 堆栈获取可观察的长度

[英]Angular 4 MEAN stack get length of Observable

I use a service function in my Angular 4 app to retrieve data from the MongoDB/Express.js backend:我在 Angular 4 应用程序中使用服务函数从 MongoDB/Express.js 后端检索数据:

getArticles() {
  return this.http.get('api/articles').map(res => res.json());
}

How do I get the length of the received array?如何获取接收到的数组的长度?

Update:更新:

I should have clarified, I needed to find a length of an Observable array.我应该澄清一下,我需要找到一个 Observable 数组的长度。

var size = Object.keys(myObj).length; . . Object.keys() 'returns an array of a given object's own enumerable properties', from MDN . Object.keys() '返回给定对象自己的可枚举属性的数组',来自MDN So if you do Object.keys(myObj) you will get an array of enumerable properties.因此,如果您执行Object.keys(myObj)您将获得一组可枚举的属性。 Doing .length on that array will provide you with the number of enumerable properties.在该数组上执行.length将为您提供可枚举属性的数量。

Found the solution here: https://stackoverflow.com/a/40237063/5861479 In my case it would be something like this:在这里找到解决方案: https : //stackoverflow.com/a/40237063/5861479在我的情况下,它会是这样的:

getArticlesLength() {
  return this.http.get('api/articles')
    .map(res => res.json()).subscribe(result => {console.log(result.length)});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM