-1
I have the following string `a+b*c` and json :
{
a: 'hello',
b: 'hello2',
c: 'hello3'.
}
我想替换字母串,使最终的字符串是 hello+hello2*hello3
具有相同字符的值替换字符串中的字符。在JSON
在Js或lodash中有没有简单的方法来做到这一点?
I have the following string `a+b*c` and json :
{
a: 'hello',
b: 'hello2',
c: 'hello3'.
}
我想替换字母串,使最终的字符串是 hello+hello2*hello3
具有相同字符的值替换字符串中的字符。在JSON
在Js或lodash中有没有简单的方法来做到这一点?
分割字符串到一个数组,然后遍历数组从相应的对象属性
var str = 'a+b*c',
params = str.split(''),
data = {
a: 'hello',
b: 'hello2',
c: 'hello3'
};
var res = params.reduce((a, c) => {
return a += data[c] ? data[c] : c;
}, '')
console.log(res)
建立新的字符串你真的不能做一个网上搜索的“替换字符串在JavaScript中“,还是一些等价的? –
@SamAxe如果您觉得这是低于您的高标准,请将其报告给mods。 – user2133404
你不应该用一个字符串替换。你应该将json解析为一个JavaScript对象并处理对象的属性。 – bhspencer