Soy autodidacta con Apps Script, por lo que generalmente aborda un problema a la vez, a medida que surge. ¡Las matrices son confusas!
Estoy usando una API para obtener la cantidad de seguidores sociales para las cuentas de Facebook, Twitter e Instagram. Aquí está el código hasta ahora. Tenga en cuenta que eliminé los detalles de la llamada API por privacidad, también usé ID de perfil falso en lo anterior, por ejemplo, 1111 = Facebook, 2222 = Twitter, etc.
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}}]
luego mapea la matriz -
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}]]
Ahora me quedo atascado, no estoy seguro de cómo obtener 'followers_count' cuando 'profile_id=1111', ¿alguien puede ayudarme? He intentado usar otra función de mapa ( var followers = dataset.map(function(ex){return [ex.dimensions.map(function(ex2){return [ex2.followers_count]}]}); ) sin embargo, esto no trabajar...
¡Cualquier sugerencia que me empuje en la dirección correcta es muy apreciada!
Si Logger.log(followers); es [[{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}]] y desea recuperar el valor lifetime_snapshot.followers_count: 407919 usando customer_profile_id: 1111 , ¿qué tal el siguiente script de muestra?
En este script de muestra, se utilizan sus valores de 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 , pensé que se recupera 407919 .console.log(res) .