【问题标题】:2D Arrays in Google Apps ScriptGoogle Apps 脚本中的二维数组
【发布时间】: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


【解决方案1】:

如果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}]] 并且您想使用customer_profile_id: 1111 检索lifetime_snapshot.followers_count: 407919 的值,那么下面的示例脚本怎么样?

示例脚本:

在此示例脚本中,使用您的 followers 值。

const profile_id = 1111; // Please set customer_profile_id you want to use.

const res = followers.reduce((ar, [a, b]) => {
  if (a["customer_profile_id"] == profile_id) {
    ar.push(b["lifetime_snapshot.followers_count"]);
  }
  return ar;
}, []);
if (res.length == 0) return;
const followers_count = res[0];
console.log(followers_count);
  • 当此脚本用于您的followers 值时,我认为407919 已检索。
  • 如果存在相同的 ID,您可以使用 console.log(res) 检索它们。

参考:

【讨论】:

  • 谢谢,这有效。我不确定我是否理解它是如何工作的,如果你有时间,我会喜欢更多关于代码如何工作的解释。谢谢!
  • @racker 感谢您的回复。我很高兴你的问题得到了解决。关于followers.reduce(,,,),在这种情况下,ar[]a, b 是数组followers 中每个元素的第一个和第二个元素。使用这些值,当customer_profile_id 的值与profile_id 相同时,将检索lifetime_snapshot.followers_count 的值。由此,得到407919。如果这个解释没有用,我很抱歉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 2020-07-26
  • 2020-12-04
相关资源
最近更新 更多