Javascript 保存变量中相等注释的数量-角度2

Javascript 保存变量中相等注释的数量-角度2,javascript,angular,Javascript,Angular,我有一个API,它返回一个JSON,注释范围从1到5,我需要在变量中存储等量的注释 例如: var n1 = 5; // Quantity of 10 notes with a value of 1 - [1,1,1,1,1] var n2 = 3; // Quantity of 3 notes with a value of 2 - [2,2,2] var n3 = 4; // Quantity of 4 notes with a value of 3 - [3,3,3,3] 我的JSON如

我有一个API,它返回一个JSON,注释范围从1到5,我需要在变量中存储等量的注释

例如:

var n1 = 5; // Quantity of 10 notes with a value of 1 - [1,1,1,1,1]
var n2 = 3; // Quantity of 3 notes with a value of 2 - [2,2,2]
var n3 = 4; // Quantity of 4 notes with a value of 3 - [3,3,3,3]
我的JSON如下所示:

[
  {
    "author": "Lorem ipsum",
    "note": "5",
    "description": "Lorem ipsum dolor sit amet"
  }
]
我使用的代码是:

numNotas() {
  this.http.get(this.url).map(res => res.json())
    .subscribe(data => {
      console.log(data);
  });
}

我尝试使用类似于
console.log(data.note)
的方法来尝试只列出数字,并且我可以尝试将相同的注释保存在相应的变量中,但是
控制台
返回了我的
未定义
。那么,我如何才能获得这些相同的注释并以正确的方式将它们保存在变量中呢?

在rxjs流中的
.map()
操作之后,尝试以下代码

您可能需要使用
import'ngrx/add/operators/reduce
导入
reduce操作员

const json=[
{注:“5”},
{注:“5”},
{注:“5”},
{注:“3”},
{注:“3”},
]
console.log(
json
.map(obj=>parseInt(obj.note))
.减少((acc,val)=>{
acc[val]++
返回acc
}, { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0})

)
请指定您在标记中使用的是angular2和rxjs。@WilomGfx是的,我是。我可以正常打印数据。我的问题是将注释保存在变量中。工作得很好!还有一个问题,我如何在另一个函数中检索变量?嗯,我需要他们来计算平均分数。如果你把它放在
map()
subscribe()
之间,你可以使用
数据访问它。1
数据[1]
。如果您还需要访问其他数据,请将其从rxjs流中删除,并将其放入
subscribe()
方法中(或任何需要的地方),然后将其分配给变量,它将与普通javascript以及rxjs一起使用。(如果你接受我的答案,如果它对你有用,那也太好了:)