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

225
Views
¿Cómo filtrar los datos JSON obtenidos?

Estoy tratando de crear un comando simple que obtenga información de imgur, encuentre las imágenes y, a partir de ahí, decida aleatoriamente qué foto mostrar. El problema que tengo es que parece que no puedo filtrar las respuestas según ciertos criterios. Aquí está el código relevante:

 async execute(interaction) { const data = await fetch( `https://imgur.com/r/${ subreddits[Math.floor(Math.random() * subreddits.length)] }/hot.json` ) .then((response) => response.json()) .then((body) => body.data) const filtered = await data.filter( (element) => element.animated == "false" ); console.log(filtered); const selected = data[Math.floor(Math.random() * data.length)]; await interaction.reply({ content: `https://imgur.com/${selected.hash}${selected.ext.replace( /\?.*/, "" )}`, }); }

Como puede ver, intenté filtrar los datos según el booleano animado que está presente en cada elemento JSON. Falso significa que no está animado, por lo tanto, es una imagen. Pero el valor filtered solo devuelve una matriz vacía [] . No estoy seguro de por qué no pasa todos los valores filtrados (imagen) como datos json. Cualquier ayuda sería apreciada.

EDITAR: Aquí está uno de los múltiples elementos de la información JSON que se incluye en data . Imagina esto pero 20x de diferentes publicaciones:

 { id: 5987473707, hash: 'jV66SOE', author: 'RushVoidStaff', account_id: null, account_url: null, title: 'Meet these three brothers! Wheels, Judas and Porridge', score: 73947, size: 1015100, views: '111639', is_album: false, album_cover: null, album_cover_width: 0, album_cover_height: 0, mimetype: 'image/jpeg', ext: '.jpg', width: 2048, height: 1536, animated: false, looping: false, reddit: '/r/kittens/comments/dmtfvc/meet_these_three_brothers_wheels_judas_and/', subreddit: 'kittens', description: '#kittens #aww', create_datetime: '2019-10-25 06:31:29', bandwidth: '105.54 GB', timestamp: '2019-10-25 06:40:10', section: 'kittens', nsfw: false, prefer_video: false, video_source: null, video_host: null, num_images: 1, in_gallery: false, favorited: false, adConfig: { safeFlags: [Array], highRiskFlags: [], unsafeFlags: [], wallUnsafeFlags: [], showsAds: true } }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El valor en el JSON es false , no "false" . Simplemente reemplace == "false" con === false . Los iguales sueltos no funcionan en booleanos ya que javascript ve "falso" como una cadena no vacía, que se convierte en verdadero.

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!