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

218
Views
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 answers
Answer question

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 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!