Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

579
Views
Matrices 2D en Google Apps Script

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!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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?

Guión 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);
  • Cuando este script se usa para sus valores de followers , pensé que se recupera 407919 .
  • Si existen los mismos ID, puede recuperarlos mediante console.log(res) .

Referencia:

  • reducir()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!