【发布时间】:2021-12-17 13:59:50
【问题描述】:
我是使用 Apps 脚本自学的,因此通常会在出现问题时一次处理一个问题。数组很混乱!
我正在使用 API 来获取 Facebook、Twitter 和 Instagram 帐户的社交关注者数量。这是到目前为止的代码。请注意,我已经删除了 API 调用细节以保护隐私,我还在上面使用了虚假的个人资料 ID,例如 1111 = Facebook、2222 = Twitter 等...
var response = UrlFetchApp.fetch(endpointUrl, params);
var jsonss = JSON.parse(response.getContentText());
var dataset = jsonss.data;
Logger.log(dataset);
[{dimensions={customer_profile_id=1111, reporting_period.by(day)=2021-10-31}, metrics={lifetime_snapshot.followers_count=407919.0}}, {dimensions={reporting_period.by(day)=2021-10-31, customer_profile_id=2222}, metrics={lifetime_snapshot.followers_count=1037310.0}}, {dimensions={reporting_period.by(day)=2021-10-31, customer_profile_id=3333}, metrics={lifetime_snapshot.followers_count=806522.0}}]
然后映射数组 -
var followers = dataset.map(function(ex){return [ex.dimensions,ex.metrics]});
Logger.log(followers);
[[{reporting_period.by(day)=2021-10-31, customer_profile_id=1111}, {lifetime_snapshot.followers_count=407919.0}], [{reporting_period.by(day)=2021-10-31, customer_profile_id=2222}, {lifetime_snapshot.followers_count=1037310.0}], [{customer_profile_id=3333, reporting_period.by(day)=2021-10-31}, {lifetime_snapshot.followers_count=806522.0}]]
现在我卡住了,我不知道当 'profile_id=1111' 时如何获得 'followers_count',有人可以帮忙吗?我尝试过使用另一个地图功能(var followers = dataset.map(function(ex){return [ex.dimensions.map(function(ex2){return [ex2.followers_count]}]});)但是这不起作用...
非常感谢任何将我推向正确方向的建议!
【问题讨论】:
-
提供
console.log(response)而不是Logger -
如果数组令人困惑,那么这是您跳过的问题。继续跳过它将继续在未来的解决方案中花费更多。数组的方法是简化代码的关键。
标签: javascript arrays google-apps-script multidimensional-array