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)

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

Leave a Comment