I have an row object say user which contains key value pair of column and value of that column. Now i want to highlight all the occurrences of a particular word in that row and then return that updated row.Reactjs table
I was trying something like this but it didn't work
const searched = searchVal;
if (searched !== "") {
Object.entries(user).forEach(([key, value]) => {
if (value.includes(searched))
{
const re = new RegExp(searched, "g");
const newText = value.replace(re, `<mark>${searched}</mark>`);
value = newText;
}
return origData[index];
}
});
}
Something like this
const searched = 'jo'
const user = {
name: "john",
lastname: "doe"
}
const highlight = (obj, search) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, value.replace(new RegExp(search, 'ig'), `<mark>${search}</mark>`)]))
console.log(highlight(user, 'jo'))