我如何使用 fb jssdk 获得所有份额的总和
how would I get the sum of all shares using fb jssdk
我调用 facebook 图 api (PageID/feed?fields=shares) 并收到此响应
` {
"data": [
{
"shares": {
"count": 1200
},
"id": "POST#0 ID GOES HERE"
},
{
"shares": {
"count": 1100
},
"id": "POST#1 ID GOES HERE"
}
],
`
我想遍历数组并获取所有份额的总和。 (本例中为 2300)
如果我使用console.log(response.data[0].shares[0].count[0]);
我得到了正确的计数 (1200),但是如果我说 20 个不同的 "shares"
,我将如何处理这个问题
可以使用ES6的reduce函数得到结果
const arr = {
"data": [{
"shares": {
"count": 1200
},
"id": "POST#0 ID GOES HERE"
},
{
"shares": {
"count": 1100
},
"id": "POST#1 ID GOES HERE"
}
]
}
const sharesSum = arr.data.reduce((acc, x) => acc + x.shares.count, 0)
console.log(sharesSum)
我调用 facebook 图 api (PageID/feed?fields=shares) 并收到此响应
` {
"data": [
{
"shares": {
"count": 1200
},
"id": "POST#0 ID GOES HERE"
},
{
"shares": {
"count": 1100
},
"id": "POST#1 ID GOES HERE"
}
],
`
我想遍历数组并获取所有份额的总和。 (本例中为 2300)
如果我使用console.log(response.data[0].shares[0].count[0]);
我得到了正确的计数 (1200),但是如果我说 20 个不同的 "shares"
可以使用ES6的reduce函数得到结果
const arr = {
"data": [{
"shares": {
"count": 1200
},
"id": "POST#0 ID GOES HERE"
},
{
"shares": {
"count": 1100
},
"id": "POST#1 ID GOES HERE"
}
]
}
const sharesSum = arr.data.reduce((acc, x) => acc + x.shares.count, 0)
console.log(sharesSum)