简体   繁体   English

如何使用angular和firebase从json值获取平均值

[英]How can I get an average from json values using angular and firebase

first of all, I'm French so all my apologies for my english... 首先,我是法国人,所以我为我的英语道歉...

I would like to calculate an average from values in json using firebase and angular. 我想使用Firebase和angular从json中的值计算平均值。 My goal is to filter with a specific id and get the rate value. 我的目标是使用特定ID进行过滤并获取费率值。

"avis" : {
"-LTMQXqMDrRPgJmtYteU" : {
  "date" : "11/11/2018",
  "description" : "jkldjflsdjfklsdjklfjkdkl",
  "id" : "-LPzmMTwf8muvAl8xhPM",
  "name" : "daniel",
  "rate" : "4"
},
"-LTM_fI6KREHqKkdcjc1" : {
  "date" : "12/12/2018",
  "description" : "jlkj",
  "id" : "-LPzmr9M6DrSCViNphj-",
  "name" : "sfgf",
  "rate" : 2
},
"-LWQJuqDJoogUcZt5KPL" : {
  "date" : "17/01/2019",
  "description" : "FFFF",
  "id" : "-LPzmr9M6DrSCViNphj-",
  "name" : "FFF",
  "rate" : 1
},

... For example, just below, I would like to calculate the rate average for id -LPzmr9M6DrSCViNphj-. ...例如,在下面,我想计算id -LPzmr9M6DrSCViNphj-的平均速率。 So I get rate 2 and 1, the average is 1.5. 因此,我得到的比率为2和1,平均值为1.5。

I create a function : 我创建一个函数:

    this.afDb.list('/avis', ref => ref.orderByChild('id').equalTo(this.item.key)).valueChanges().
subscribe(
  result => {
    console.log('result: ', result);
  });

But when I do that, I have all values and I would like only rate values... 但是当我这样做时,我拥有所有值,并且我只希望对值进行评分...

I don't know if it is clear... 我不知道是否清楚...

Thank you very much for your support ! 非常感谢您的支持 !

const total = avis.reduce((sum, item) => sum + item.rate, 0);

console.log(total);

OR 要么

this.http.get('/avis')
.map(response.avis)
.map(res => res.rate)
.reduce((a, b) => a + b)
.subscribe(res => console.log(res))

You can use .map() & reduce() to achieve your avg value. 您可以使用.map()reduce()来实现平均值。

let listData = this.afDb.list('/avis', {
            query: {
                orderByChild: 'id',
                equalTo: this.item.key,
            }
        }).valueChanges();

let avg = listData.map(res => res["rate"]).reduce((a, b) => a + b)/listData.length;

Or with the same code you can use like below code to get average. 或者使用相同的代码,您可以使用下面的代码来求平均值。

this.afDb.list('/avis', ref => ref.orderByChild('id').equalTo(this.item.key)).valueChanges().
subscribe(
  result => {

    console.log('result: ', result);
    let avg = result.map(res => res. rate).reduce((a, b) => a + b)/result.length;

  });

By .map() & .reduce() you will get your total rating and then divide it with array length . 通过.map().reduce()您将获得总评分,然后将其除以数组length

Ref Link : https://www.jstips.co/en/javascript/array-average-and-median/ 参考链接: https : //www.jstips.co/en/javascript/array-average-and-median/

Hope this will helps! 希望这会有所帮助!

@CodeChanger : @CodeChanger:

this.listData = this.afDb.list('avis', ref => ref.orderByChild('id').equalTo(this.item.key)).valueChanges();
this.avg = this.listData.map(res => res.rate).reduce((a, b) => a + b) / this.listData.length;

There is no error after the compilation but when I launch my app, I have this error: 编译后没有错误,但是当我启动应用程序时,出现以下错误:

ERROR Error: Uncaught (in promise): TypeError: this.listData.map is not a function 错误错误:未捕获(承诺):TypeError:this.listData.map不是函数

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

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