用 json 中相同字符的值替换字符串字符
Replace string characters with value of the same character in json
I have the following string `a+b*c` and json :
{
a: 'hello',
b: 'hello2',
c: 'hello3'.
}
我想替换字符串中的字母,这样最终的字符串就是
hello+hello2*hello3
.
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)
I have the following string `a+b*c` and json :
{
a: 'hello',
b: 'hello2',
c: 'hello3'.
}
我想替换字符串中的字母,这样最终的字符串就是
hello+hello2*hello3
.
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)