0
작업이 간단합니다. 식별자와 객체 배열()의 문자열을 대체 할 것으로 예상되는 함수를 작성했습니다. 자바 스크립트 바꾸기 기능이 새 값을 반환하지 않음
업데이트를 확인하는 경우
var replacer = function(original, replacement) {
console.log(replacement)
_.each(replacement, (value, key) => {
original = original.replace('${' + key+'}', value)
});
return original;
}
console.clear();
var asString = "This is my email ${email} & ${mobile}";
var replacement = {"email":"[email protected]", "mobile":"999999999"}
asString = replacer(asString, replacement);
console.log(asString) // Here i am getting it properly replaced
var replacement ={"email":"[email protected]", "mobile":"0000000"}
asString2 = replacer(asString, replacement);
console.log(asString2) // Here i am still getting old value not new value
,
http://jsfiddle.net/1gc9xka8/1
이 난 변수를 변경하지만 여전히 나는 이전 값을 얻는다.
무엇이 잘못 되었습니까?
그냥 .. 시험 대신 대체 문자열을 전달하고 있습니다하지만 당신은 원래 ... original.replace이 lodash없이 원래 – littleboots
를 돌연변이됩니다 재 할당 할 필요가없는 , 바닐라 :'Object.keys (replacement) .reduce ((result, current) => result.replace ("$ {"+ 현재 + "}", 대체 [현재]), asString)' –