Google Apps 脚本中的二维数组
2D Arrays in Google Apps Script
我是自学 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]}]});
)但是这不起作用...
非常感谢任何能将我推向正确方向的建议!
如果 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)
检索它们。
参考:
我是自学 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]}]});
)但是这不起作用...
非常感谢任何能将我推向正确方向的建议!
如果 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)
检索它们。