Javascript 如何替换对象的深度值?

Javascript 如何替换对象的深度值?,javascript,Javascript,下面是一个带有深道具的物体 如何替换以下属性的值 name: 'name' 与: name: 'replaced_name' 预期结果: const sample_object = { prop1: { subprop1: { anothersubprop1: { properties: { deepProp1: { name

下面是一个带有深道具的物体

如何替换以下属性的值

name: 'name'
与:

name: 'replaced_name'
预期结果:

const sample_object = {
    prop1: {
        subprop1: {
            anothersubprop1: {
                properties: {
                    deepProp1: {
                        name: 'replaced_name' 
                    }
                },
                '$ref': {

                },
                name: 'replaced_name'
            }
        }
    },
        prop2: {
        subprop1: {
            anothersubprop1: {
                properties: {
                    deepProp1: {
                        name: 'replaced_name' 
                    }
                },
                '$ref': {

                },
                name: 'replaced_name'
            },
            name: 'replaced_name'
        },
                subprop2: {
            anothersubprop1: {
                properties: {
                    deepProp1: {
                        name: 'replaced_name' 
                    },
                    deepProp2: {
                        name: 'replaced_name' 
                    }
                },
                '$ref': {

                },
                name: 'replaced_name'
            },
                        anothersubprop2: {
                properties: {
                    deepProp1: {
                        name: 'replaced_name' 
                    },
                    deepProp2: {
                        name: 'replaced_name' 
                    }
                },
                '$ref': {

                },
                name: 'replaced_name'
            },

            name: 'replaced_name'
            ...
        }
    },
}

如果可以使用lodash或任何其他类似库,请建议
或任何自定义代码

您可以编写递归帮助程序

import _ from 'lodash'

const = deepReplaceRecursive(old, new, obj) = {
  const new = _.clone(obj);

  _.each(object, (val, key) => {
    if (val === old) {
      // replace val if it's equal to the old value
      newObj[key] = new;
    } else if (typeof(val) === 'object' || typeof(val) === 'array') {
      // if val has nested values, make recursive call
      newObj[key] = deepReplaceRecursive(old, new, val);
    }
  });

  return newObj;
}

const object = { ... }
const newObject = deepReplaceRecursive("name", "replaceName", object)

您可以编写递归帮助器

import _ from 'lodash'

const = deepReplaceRecursive(old, new, obj) = {
  const new = _.clone(obj);

  _.each(object, (val, key) => {
    if (val === old) {
      // replace val if it's equal to the old value
      newObj[key] = new;
    } else if (typeof(val) === 'object' || typeof(val) === 'array') {
      // if val has nested values, make recursive call
      newObj[key] = deepReplaceRecursive(old, new, val);
    }
  });

  return newObj;
}

const object = { ... }
const newObject = deepReplaceRecursive("name", "replaceName", object)

您可以将对象更改为string,然后用新值替换旧值,然后使用
JSON.parse
将字符串重新设置为对象

例如:

let str=JSON.stringify(示例_对象)
str=str.replace(/“name”:“name”/g”,“name”:“replaced_name”)//g->for replace所有出现的`name:name'`
console.log(JSON.parse(str))

您可以将对象更改为字符串,然后用新值替换旧值,然后使用
JSON.parse
将字符串重新设置为对象

例如:

let str=JSON.stringify(示例_对象)
str=str.replace(/“name”:“name”/g”,“name”:“replaced_name”)//g->for replace所有出现的`name:name'`
console.log(JSON.parse(str))