Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
How to filter fetched JSON data?

I'm trying to make a simple command that pulls info from imgur, finds the images, and from there randomly decides which photo to show. The problem I'm having is that I can't seem to filter the responses based on certain criteria. Here is the relevant code:

  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(
        /\?.*/,
        ""
      )}`,
    });
  }

As you can see, I tried filtering the data based on the animated boolean that is present on each JSON element. False means it is not animated, hence it is an image. But the filtered value just returns an empty array []. I'm not sure why it's not passing all of the filtered (image) values as json data. Any help would be appreciated.

EDIT: Here is one of the multiple elements of JSON info that goes into data. Imagine this but 20x from different posts:

{
    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 Respuestas
Responde la pregunta

0

The value in the JSON is false, not "false". Just replace == "false" with === false. The loose equals doesn't work on booleans as javascript sees "false" as a non-empty string, which gets converted to true.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda