我是新的反应,并试图突出显示基于数组关键字的JSX表达式中的文本。
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"]}发布于 2022-05-17 10:12:48
您可以创建一组搜索单词,检查该文本在集合中是否可用,也可以使用数组,但与集合相比要慢一些。
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>
)这应该能解决你的问题,
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>
);https://stackoverflow.com/questions/72271998
复制相似问题