问题描述
我想检查帖子中是否有用户提及,并插入用户名以及指向用户个人资料的链接。它适用于这样的首次提及:
if (mentions.length > 0) {
const splitSearch = '@' + mentions[0].user.name;
children = text.split(splitSearch);
for (let i = children.length - 1; i > 0; i--) {
const a = (
<a href={`profile/${mentions[0].user._id}`}>@{mentions[0].user.name}</a>
);
children.splice(i,a);
}
}
我尝试了for循环:
if (mentions.length > 0) {
for (let index = 0; index < mentions.length; index++) {
const splitSearch = '@' + mentions[index].user.name;
children = text.split(splitSearch);
for (let i = children.length - 1; i > 0; i--) {
const a = (
<a href={`profile/${mentions[index].user._id}`}>
@{mentions[index].user.name}
</a>
);
children.splice(i,a);
}
}
}
我认为问题在于结果没有保存,只是被下一个环形螺母覆盖,我不知道如何解决。
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)