[英]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
,下面的示例脚本怎么样?
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);
followers
, I thought that 407919
is retrieved.followers
值时,我认为检索到了407919
。console.log(res)
.console.log(res)
检索它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.