1

I have a React component where I'm getting back a group of items from an API call.

Each item has an attribute groupName which could be any number of values.

What I'm trying to do is take the original array and group the items in that array by shared value and push them into their own array.

The issue I'm having is that I have no idea how to dynamically name and create the array for these items.

I know how to statically declare array names and then push items into those pre established containers, but dynamically creating an array for pushing items to has me completely stumped.

This was my initial attempt but it's all hard coded:

function renderList(items) {
  const marvel = [];
  const dc = [];

  if (!items) return null;

  data.map((items) => {
    switch (item.groupName) {
      case "Marvel Characters":
        return marvel.push(<Item />);
      case "DC Characters":
        return dc.push(<Item />);
      default:
        return null;
    }
  });

  return resources;
}

My data looks something like this where there could be any number of groupName arrays that need to be created:

export default [
  {
    name: "Iron Man",
    subtitle: "Inventor Tony Stark",
    img:
      "https://www.denofgeek.com/wp-content/uploads/2019/02/mcu-1-iron-man.jpg?resize=768%2C432",
    groupName: "Marvel Characters"
  },
  {
    name: "Incredible Hulk",
    subtitle: "Bruce Banner",
    img:
      "https://lh3.googleusercontent.com/proxy/-jHnFcGLqlxjdOl9Mf99UPBk4XJKcQ1Hsv7lPYEs8Vai874sW0l5TUwn3acriwGpE36aUDPpZHPFzccRUt7b7POGOWCFIbgYomTO9bDCXF0eovxFGdr_D3P-0wfLnkUMOOJDG09MgAzqSCbiDq-A",
    groupName: "Marvel Characters"
  },
  {
    title: "Kamahl, Fist Of Krosa",
    link:
      "https://gatherer.wizards.com/pages/card/Details.aspx?multiverseid=220490",
    groupName: "Magic Cards"
  },
  {
    title: "Seedborn Muse",
    link:
      "https://gatherer.wizards.com/pages/card/Details.aspx?multiverseid=446180",
    groupName: "Magic Cards"
  },
  {
    title: "Silvos, Rogue Elemental",
    link:
      "https://gatherer.wizards.com/pages/card/Details.aspx?multiverseid=413728",
    groupName: "Magic Cards"
  },
  {
    name: "Batman",
    subtitle: "Bruce Wayne",
    img:
      "https://static.wikia.nocookie.net/marvel_dc/images/a/a6/Batman_Vol_2_2_Variant_Textless.jpg/revision/latest/top-crop/width/360/height/450?cb=20120228075313",
    groupName: "DC Characters"
  },
  {
    name: "Martian Manhunter",
    subtitle: "J'onn J'onzz",
    img:
      "https://cdn.flickeringmyth.com/wp-content/uploads/2021/03/Martian-Manhunter-600x338.png"
  },
  {
    name: "The Flash",
    subtitle: "Barry Allen",
    img:
      "https://www.denofgeek.com/wp-content/uploads/2018/07/the-flash-heroes-in-crisis.jpg?resize=768%2C432",
    groupName: "DC Characters"
  },
  {
    name: "Green Lantern",
    subtitle: "Hal Jordan",
    img:
      "https://img3.looper.com/img/gallery/the-history-of-the-green-lantern-corps-explained/l-intro-1604675887.jpg",
    groupName: "DC Characters"
  },
  {
    name: "Godzilla",
    img:
      "https://www.denofgeek.com/wp-content/uploads/2019/05/godzillakingofmonsters-2.jpg?resize=768%2C432",
    groupName: "Kaiju and Mechs"
  },
  {
    name: "Hunter Vertigo",
    img: "https://i.ytimg.com/vi/7F-iZYAqSbw/maxresdefault.jpg",
    groupName: "Kaiju and Mechs"
  },
  {
    name: "Eva Unit 01",
    img:
      "https://images.squarespace-cdn.com/content/v1/57e05e534402434aa0f846c2/1561068519775-WCQ7AB9M9UQ49IIMQCJF/ke17ZwdGBToddI8pDm48kCIGnI8z6S8cIiNA1oCBWdIUqsxRUqqbr1mOJYKfIPR7LoDQ9mXPOjoJoqy81S2I8N_N4V1vUb5AoIIIbLZhVYxCRW4BPu10St3TBAUQYVKcHbykkH9LqPbyzX2FjgPseW488gwsivinSTmyNqjLP1-eucEtowexzXiIXHjoU3U1/neon-genesis-evangelion-header-1_1050_591_81_s_c1.jpg?format=1000w",
    groupName: "Kaiju and Mechs"
  },
  {
    name: "Gamera",
    img:
      "https://static.wikia.nocookie.net/legendary-series-monsterverse/images/1/12/Dd7xpqr-da07f2b8-d83b-4ac3-9a5d-22185651a474_%281%29.png/revision/latest?cb=20200516210147",
    groupName: "Kaiju and Mechs"
  },
  {
    title: "Descender",
    link: "https://imagecomics.com/comics/series/descender",
    groupName: "Comic Books"
  },
  {
    title: "The Invisibles",
    link: "https://en.wikipedia.org/wiki/The_Invisibles",
    groupName: "Comic Books"
  }
];
Josh
  • 1,165
  • 3
  • 24
  • 44
  • Have you tried to use .filter on your array to filter by specific values? Filter will create a new array with the filtered data. – Stephanieraymos Apr 02 '21 at 23:14

0 Answers0