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)
484537 219709Hi, you used to write excellent posts, but the last several posts have been kinda boring I miss your wonderful posts. Past couple of posts are just a bit bit out of track! 454516
183724 66928The electronic cigarette uses a battery and a small heating component the vaporize the e-liquid. This vapor can then be inhaled and exhaled 428826
880796 508848Hey, you?re the goto expert. Thanks for haingng out here. 511628
131814 297381I agree completely with what you said. Wonderful Stuff. Keep it going.. 418354
640137 520469I gotta favorite this internet site it seems extremely beneficial . 395611
Hello. And Bye.