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

120
Vistas
Highlight React JSX element for array of keywords

I am new to React and am attempting to highlight the text in a JSX expression based on the keywords from an array.

return <RightWrapper>
{value.sort((a, b) => a - b.id).map(transcript => <p key={transcript.id}>{transcript.createdAt+"  "+ transcript.Transcription +" "} </p> )}
</RightWrapper>

searchWords={["tech", "deck", "financial"]}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can create a set of search words, Check if that text is available in a set or not, You can do using an array also but it's slower compared to the set.

const set = new Set(["tech", "deck", "financial"]);
const arr =  value.sort((a, b) => a.id - b.id)

return ( 
  <RightWrapper>
  {
    arr.map(transcript => (
      <p 
        key={transcript.id}
        style={{
          color: set.has(transcript.id) ? 'red' : 'black'
        }}
        >{transcript.createdAt + "  " + transcript.Transcription + " "} </p>
    ))
  }
  </RightWrapper>
)

This should solve your problem,

const set = new Set(["tech", "deck", "financial"]);
const arr = value.sort((a, b) => a.id - b.id);

function createMarkup(transcript) {
  const words = ["tech", "deck", "financial"];
  let str = `${transcript.createdAt} ${transcript}`;
  words.forEach((word) => {
    if (str.includes(word)) {
      str = str.replaceAll(word, `<span class="highlight">${word}</span>`);
    }
  });

  return { __html: str };
}

return (
  <RightWrapper>
    {arr.map((transcript) => (
      <p
        dangerouslySetInnerHTML={createMarkup(transcript.Transcription)}
        key={transcript.id}
      ></p>
    ))}
  </RightWrapper>
);
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