替换对象(和/或数组)中的所有字符串实例 - JavaScript

And*_*nes 6 javascript string replace object

搜索未知深度和属性的JavaScript对象并替换给定字符串的所有实例的最佳方法是什么?

这有效,但这是最好的方法吗?

var obj = {
   'a' : 'The fooman poured the drinks.',
   'b' : {
      'c' : 'Dogs say fook, but what does the fox say?'
   }
}

console.log (JSON.parse(JSON.stringify(obj).replace(/foo/g, 'bar')));
Run Code Online (Sandbox Code Playgroud)

小提琴:http://jsfiddle.net/93Uf4/3/

Ing*_*ürk 7

在您提出自己的方式旁边,这是一个经典的循环方法。正如有人在评论中提到的那样,它更稳定,因为在尝试解析它时,您不会冒着搞砸对象并抛出错误的风险。另一方面,出现了一些问题(见底部)。

但是要小心,因为needle将用作正则表达式。您可能需要考虑在其中添加某种引用

我希望我没有忽略任何东西,所以测试它并玩弄它。在这里你可以找到一个fiddle.

/**
  * Replaces all occurrences of needle (interpreted as a regular expression with replacement and returns the new object.
  * 
  * @param entity The object on which the replacements should be applied to
  * @param needle The search phrase (as a regular expression)
  * @param replacement Replacement value
  * @param affectsKeys[optional=true] Whether keys should be replaced
  * @param affectsValues[optional=true] Whether values should be replaced
  */
Object.replaceAll = function (entity, needle, replacement, affectsKeys, affectsValues) {
    affectsKeys = typeof affectsKeys === "undefined" ? true : affectsKeys;
    affectsValues = typeof affectsValues === "undefined" ? true : affectsValues;

    var newEntity = {},
        regExp = new RegExp( needle, 'g' );
    for( var property in entity ) {
        if( !entity.hasOwnProperty( property ) ) {
            continue;
        }

        var value = entity[property],
            newProperty = property;

        if( affectsKeys ) {
            newProperty = property.replace( regExp, replacement );
        }

        if( affectsValues ) {
            if( typeof value === "object" ) {
                value = Object.replaceAll( value, needle, replacement, affectsKeys, affectsValues );
            } else if( typeof value === "string" ) {
                value = value.replace( regExp, replacement );
            }
        }

        newEntity[newProperty] = value;
    }

    return newEntity;
};
Run Code Online (Sandbox Code Playgroud)

最后两个参数是可选的,所以可以这样调用它:

var replaced = Object.replaceAll( { fooman: "The dog is fooking" }, "foo", "bar" );
Run Code Online (Sandbox Code Playgroud)

但是,仍然存在边缘情况,不清楚应该发生什么。例如:

// do you expect it to stay undefined or change type and become "undebazed"?
console.log( Object.replaceAll( { x: undefined }, "fin", "baz" ) );

// null or "nalala"?
console.log( Object.replaceAll( { x: null }, "ull", "alala" ) );
Run Code Online (Sandbox Code Playgroud)

或者

// true or false?
console.log( Object.replaceAll( { x: true }, "true", "false" ) );

// true or "foo"?
console.log( Object.replaceAll( { x: true }, "true", "foo" ) );
Run Code Online (Sandbox Code Playgroud)

数字也一样

// 1337 or 1007?
console.log( Object.replaceAll( { x: 1337 }, "33", "00" ) );

// 1337 or "1foo7"
console.log( Object.replaceAll( { x: 1337 }, "33", "foo" ) );
Run Code Online (Sandbox Code Playgroud)

这些情况目前都没有在我的方法中处理——只会触及对象(用于嵌套)和字符串。