Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

283
Vistas
How can I dynamically map icons with values i'm getting from an array of objects from a third party API

I'm creating a react app which lists games from a third party API. This is my card component.

In place of the textual names of platforms, i want to display icons of said platforms, for which I'm using react-icons. How can i dynamically map platform names to an icon? This is what i'm passing in the plat prop:

this is what i'm passing in the plat prop

I'm passing the values to my card component via props:

          <SearchRes 
            key={game.name} 
            title={game.name} 
            rating={game.metacritic} 
            genre={game.genres.map(genre=>genre.name+" ")} 
            imglnk={(game.background_image===null)?"":game.background_image}
            gameid={game.id}
            slug={game.slug}
            plat={game.parent_platforms}
            />

I've tried this, but then the question is how can I use it dynamically, as each game will have different sets of platforms:

<p>{platform.map(icon=>icon.platform.id===4?<FaWindows key="1"/>:null)}</p>

(used this snippet when i passed the whole platform array in prop)

Edit: I tried the hashmap by implementing this:

const iconMap = {
        1: <FaWindows key="1"/>,
        2: <FaPlaystation key="2"/>,
        3: <FaXbox key="3"/>,
        4: <FaAppStoreIos key="4"/>,
        5: <FaApple key="5"/>,
        6: <FaLinux key="6"/>,
        7: <SiNintendoswitch key="7"/>,
        8: <FaAndroid key="8"/>
    }
const icon = iconMap[plat.platform.id] || null;

but i'm getting this error: TypeError: Cannot read properties of undefined (reading 'id') error

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

A simple approach is to keep a hash map where the key will be the platform id and the value can be the React component (icon) you want to display.

Example in code:

const iconMap = {
    4: <FaWindows key="1"/>,
    5: <p>Hello world</p>
}

Then usage can be a simple as:

const icon = iconMap[platform.id] || null;
return (
  <div className='icon-wrapper'>
     {icon}
  </div>
)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda