简体   繁体   English

Google Apps 脚本中的二维数组

[英]2D Arrays in Google Apps Script

I am self taught with Apps Script, so generally approach one problem at a time, as it comes up.我使用 Apps Script 自学,所以通常一次解决一个问题,当它出现时。 Arrays are confusing!数组令人困惑!

I am using an API to get the number of social followers for Facebook, Twitter and Instagram accounts.我正在使用 API 来获取 Facebook、Twitter 和 Instagram 帐户的社交关注者数量。 Here is the code so far.这是到目前为止的代码。 Note I have removed the API call specifics for privacy, I have also used fake profile ID's in the above, for example 1111 = Facebook, 2222 = Twitter, etc...注意我已经删除了隐私的 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=3785690.0, 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=3785693.0}, metrics={lifetime_snapshot.followers_count=1037310.0}}, {dimensions={reporting_period.by(day)=2021-10-31, customer_profile_id=3785700.0}, metrics={lifetime_snapshot.followers_count=806522.0}}]

then map the array -然后映射数组 -

  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}]]

Now I get stuck, I am not sure how to get 'followers_count' when 'profile_id=1111', can someone please help?现在我卡住了,我不确定如何在“profile_id=1111”时获得“followers_count”,有人可以帮忙吗? I have tried using another map function ( var followers = dataset.map(function(ex){return [ex.dimensions.map(function(ex2){return [ex2.followers_count]}]}); ) however this doesn't work...我尝试使用另一个地图函数( var followers = dataset.map(function(ex){return [ex.dimensions.map(function(ex2){return [ex2.followers_count]}]}); )但是这没有工作...

Any suggestions to push me in the right direction is very much appreciated!非常感谢任何将我推向正确方向的建议!

If Logger.log(followers);如果Logger.log(followers); is [[{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}]] and you want to retrieve the value of lifetime_snapshot.followers_count: 407919 by using customer_profile_id: 1111 , how about the following sample script?[[{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}]]和你想要获取的lifetime_snapshot.followers_count: 407919的值lifetime_snapshot.followers_count: 407919通过使用customer_profile_id: 1111 ,下面的示例脚本怎么样?

Sample script:示例脚本:

In this sample script, your values of followers is used.在此示例脚本中,使用了您的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);
  • When this script is used for your values of followers , I thought that 407919 is retrieved.当此脚本用于您的followers值时,我认为检索到了407919
  • If the same IDs are existing, you can retrieve them using console.log(res) .如果存在相同的 ID,您可以使用console.log(res)检索它们。

Reference:参考:

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

相关问题 如何比较两个 2d Arrays 并按特定顺序返回结果(Google Apps 脚本)? - How to compare two 2d Arrays and return the result in a specific order (Google Apps Script)? Google Apps脚本/ JavaScript二维数组错误 - Google apps script / javascript 2D array typeerror Google Apps 脚本中二维数组大小的异常错误 - Exception error with 2D array size in Google Apps Script Google 应用程序脚本 - 映射数组 - Google apps script - Mapping arrays Google Apps脚本和Google Spreadsheet中的数组问题 - Problems with arrays in Google Apps Script and Google Spreadsheet 使用 Google Apps 脚本循环遍历数据集时如何构建二维数组? - How to build 2D array when looping through sets of data using Google Apps Script? Google Apps 脚本:如何从二维数组中删除特定字符 - Google Apps Script: How to remove specific characters from a 2D array 两列中的谷歌应用程序脚本搜索将匹配行作为二维数组 - Google apps script search in two columns get matching rows as 2D array Google Apps 脚本:如何在二维数组上运行拆分 function 并保持列号相同 - Google Apps Script: How to run split function on 2D array and keep column numbers the same 比较两个二维数组成对的字符串元素,而不管 Google Apps 脚本中差异的顺序 - Compare two, 2D Array paired string elements regardless of order for differences within Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM