filtering and array of objects by an array of string in the object

I have an array of objects, on the objects there is a key of gender with an array of strings. The gender array is empty [] or has one string with either ["men"] or ["women"]. I’m trying sort the array of objects into an array depending on what gender is active. I.e if the gender men is active, all the objects with gender: ["men"] should be filtered into the array. Most importantly, any objects that have an empty array, like gender: [] should also be included in both the men and women arrays. However, i’m having trouble getting this to work, my code is currently returning an array of undefineds. Any help would be greatly appreciated.

const activeGender = "women"

const images = [
    {
      "gender": [],
      "image": {
        "_id": "ac0611e600fd31bb3dc87f4b514d0b80erf8fa3b5d-f2759x4139-jpg",
        "url": "https://cdn.example.com/images/zjeoztkz/production/ac0611e600fd31bb3dc87f4b514dfr0b80f8fa3b5d-2759x4139.jpg"
      }
    },
    {
      "gender": ["women"],
      "image": {
        "_id": "ac0611e600fd31bb3dc87f4b514d0b80f8fa3b5d-2759x4139-jpg",
        "url": "https://cdn.example.com/images/zjeoztkz/production/ac0611e600fd31bb3dc87f4b514d0b80f8fa3b5d-2759x4139.jpg"
      }
    },
    {
      "gender": ["men"],
      "image": {
        "_id": "4b733a931e13d7cdf77c200d0eac94b55caee89e-3766x5649-jpg",
        "url": "https://cdn.example.com/images/zjeoztkz/production/4b733a931e13d7cdf77c200d0eac94b55caee89e-3766x5649.jpg"
      }
    },
    {
      "gender": ["men"],
      "image": {
        "_id": "f2a4ce65ce4f671c6aec4d9b6b8b2bce7b4a1e7a-5472x3648-jpg",
        "url": "https://cdn.example.com/images/zjeoztkz/production/f2a4ce65ce4f671c6aec4d9b6b8b2bce7b4a1e7a-5472x3648.jpg"
      }
    },
    {
      "gender": ["women"],
      "image": {
        "_id": "31ab7886b5b2164e245ffb41facc01c0ac66f60f-1962x2942-jpg",
        "url": "https://cdn.example.com/images/zjeoztkz/production/31ab7886b5b2164e245ffb41facc01c0ac66f60f-1962x2942.jpg"
      }
    },
    {
      "gender": ["women"],
      "image": {
        "_id": "c577801db926fd058142513dce0b834eb8dc9e16-2432x3648-jpg",
        "url": "https://cdn.example.com/images/zjeoztkz/production/c577801db926fd058142513dce0b834eb8dc9e16-2432x3648.jpg"
      }
    },
    {
      "gender": ["women"],
      "image": {
        "_id": "e6e16e0da123fa510efe2ccb1269f18afa8cda64-5472x3648-jpg",
        "url": "https://cdn.example.com/images/zjeoztkz/production/e6e16e0da123fa510efe2ccb1269f18afa8cda64-5472x3648.jpg"
      }
    }
  ]
  

const getMens = (items) =>
  items
    .filter((i) => i.gender.length === 0 || i.gender.includes("men"))
    .map((i) => i.images)
    .reduce((a, b) => a.concat(b), []);
    
const getWomens = (items) =>
  items
    .filter((i) => i.gender.length === 0 || i.gender.includes("women"))
    .map((i) => i.images)
    .reduce((a, b) => a.concat(b), []);
    
  const filteredImages =
    activeGender === "women"
      ? getWomens(images)
      : getMens(images);
      
      console.log(filteredImages)

10 thoughts on “filtering and array of objects by an array of string in the object”

  1. If you do, you agree with George Herbert’s famous aphorism from his book, Outlandish Proverbs.
    Examples of Aphorisms for Success
    George Washington is known for his wise sayings.
    Let me ask you.
    Sometimes, though.
    The original saying was, Eat an apple on going to bed, and you’ll keep the doctor from earning his bread.
    So my advice.
    They’re written in countless books and passed down as folk wisdom.
    Now compare that proverb to this famous aphorism.
    The original dictum said, A penny spar’d is twice got, but it’s adapted over the years for modern English.
    Take a look.
    This aphorism is short and sweet, but it teaches us a valuable truth.
    That’s not what you expected, was it.
    This quote came from Wales, first appearing in an 1866 publication.
    Then use it as a guideline to stay focused on your general theme.
    Today, I’ll define aphorism and show you how these handy little sayings make your writing more memorable.

    Reply
  2. 949772 594325Right after study several with the weblog articles for your internet web site now, and i also genuinely such as your strategy for blogging. I bookmarked it to my bookmark internet web site list and are checking back soon. Pls take a look at my web page in addition and tell me what you believe. 177685

    Reply
  3. Hello! I just wanted to ask if you ever have any issues with
    hackers? My last blog (wordpress) was hacked and I ended up losing a
    few months of hard work due to no data backup.
    Do you have any solutions to stop hackers?

    Reply
  4. 472378 537472Fantastic post nonetheless , I was wanting to know in the event you could write a litte far more on this subject? Id be really thankful if you could elaborate a bit bit further. Bless you! 774381

    Reply

Leave a Comment