Javascript 将${dummy}替换为来自JSON的字符串中的变量值
我有一个Json文件Javascript 将${dummy}替换为来自JSON的字符串中的变量值,javascript,Javascript,我有一个Json文件 [ "Cooling": { "id": 1, "title": "Cooling", "description": "Lorem Ipsum is simply ${dummy} text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an u
[
"Cooling":
{
"id": 1,
"title": "Cooling",
"description": "Lorem Ipsum is simply ${dummy} text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took"
}
]
在渲染之前,我想根据某些条件将${dummy}替换为其他内容。在react中这可能吗
我正在我的react App.js组件中导入此文件
并显示为:
return (
<div>
<div className="row">
{Json.map(item => (
<div className="col-md-1">
<hr />
<p key={item.id}>
{item.title}
</p>
<p>{item.description}</p>
</div>
))}
</div>
</div>
);
返回(
{Json.map(项=>(
{item.title}
{item.description}
))}
);
是否可以在呈现前替换item.description中的${dummy}?您可以使用字符串替换
return (
<div>
<div className="row">
{Json.map(item => (
<div className="col-md-1">
<hr />
<p key={item.id}>
{item.title}
</p>
<p>
{
item.description.replace('${dummy}', `${your_variable}`)
}
</p>
</div>
))}
</div>
</div>
);
返回(
{Json.map(项=>(
{item.title}
{
item.description.replace('${dummy}','${your_variable}`)
}
))}
);
在我看来就像一个简单的字符串替换,你试过什么?@DanD你是说像什么${expression}
?我确实试过了,但是JSON文件只是说了错误的语法。我想要类似于{replacestring}的东西,其中replace string是一个prop项。replace(${expression}),“我的文本”)
是一个起点@DanD ahh,所以您只是将${expression}视为一个字符串。是的,那会有用的。我只是假设react会有一些非常不同的东西,因为我是新来的react….:p但这肯定行,谢谢你可能的副本