从给定的一个(JavaScript)创建更新和编辑的嵌套对象

发布于 2025-01-21 09:22:06 字数 1187 浏览 0 评论 0原文

我得到了一个看起来像这样的架构对象:

const schema = {
social: {
    facebook: 'someValue',
    twitter: {
        department: {
            departmentImage: {
                editable: 'someValue'
            }
         }
      }
   }
};

ediable属性指示我要编辑的值,并且可能出现在对象中的几个嵌套位置中。

我的编辑方法是递归创建一个新的对象,该对象是原始的精确副本,并在我遇到的新值可编辑中填充了一个新值。 这样:

const formatSchema = (schema, data, formattedSchema = {}) => {
  for (const schemaKey in schema) {
    const firstKey = Object.keys(schema[schemaKey])[0];
            
    if (schema[schemaKey] instanceof Object) {
        formattedSchema[schemaKey] = schema[schemaKey];
        formatschema(schema[schemaKey], data, formattedSchema[schemaKey]);
    }
    if (schema[schemaKey] instanceof Object && firstKey === 'editable') {
       *replacing data logic*
       formattedSchema[schemaKey] = ...*replacingData*;
       formatschema(schema[schemaKey], data, formattedSchema[schemaKey]);
    } else {
        formattedSchema[schemaKey] = schema[schemaKey];
    }
  }
 return formattedSchema;
};

但是我觉得这个解决方案可能会效率低下,因为我从头开始创建对象的每一点,这将每天发生数千次。

有没有办法做得更好?

I got a schema object that looks like this:

const schema = {
social: {
    facebook: 'someValue',
    twitter: {
        department: {
            departmentImage: {
                editable: 'someValue'
            }
         }
      }
   }
};

The editable property indicates a value that I want to edit, and may appear in several nested locations in the object.

My approach to edit it is to recursively create a new object who is an exact copy of the original, and populate a new value where I encounter editable.
Like this:

const formatSchema = (schema, data, formattedSchema = {}) => {
  for (const schemaKey in schema) {
    const firstKey = Object.keys(schema[schemaKey])[0];
            
    if (schema[schemaKey] instanceof Object) {
        formattedSchema[schemaKey] = schema[schemaKey];
        formatschema(schema[schemaKey], data, formattedSchema[schemaKey]);
    }
    if (schema[schemaKey] instanceof Object && firstKey === 'editable') {
       *replacing data logic*
       formattedSchema[schemaKey] = ...*replacingData*;
       formatschema(schema[schemaKey], data, formattedSchema[schemaKey]);
    } else {
        formattedSchema[schemaKey] = schema[schemaKey];
    }
  }
 return formattedSchema;
};

But I feel this solution may be inefficient as I create every single bit of the object from scratch and this would happen thousands of times a day.

Is there a way to do it better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萝莉病 2025-01-28 09:22:06

这是适用于任何本机输入类型的递归不变更新。不必担心在这里的性能,因为即使您的对象有成千上万个字段,它也很快。让我知道这适合您,如果需要,我可以进行更改 -

function update(t, func) {
  switch (t?.constructor) {
    case Object:
      return Object.fromEntries(
        Object.entries(t).map(([k,v]) =>
          [k, func([k, update(v, func)])]
        )
      )
    case Array:
      return t.map((v, k) => func([k, update(v, func)]))
    default:
      return func([null, t])
  }
}

const schema = {
  social: {
    facebook: 'someValue',
    twitter: {
      department: {
        departmentImage: {
            editable: 'someValue'
        }
      },
      someArr: [{ editable: 1 }, { editable: 2 }, { hello: "world" }]
    },
  }
}

console.log(update(schema, ([k,v]) =>
  k == "editable" ? "✅" : v
))
.as-console-wrapper {min-height: 100% !important; top: 0}

{
  "social": {
    "facebook": "someValue",
    "twitter": {
      "department": {
        "departmentImage": {
          "editable": "✅"
        }
      },
      "someArr": [
        {
          "editable": "✅"
        },
        {
          "editable": "✅"
        },
        {
          "hello": "world"
        }
      ]
    }
  }
}

Here's a recursive immutable update that works for any native input type. Don't worry about performance here as it's plenty fast, even if your object has thousands of fields. Let me know how this suits you and I can make a change if it's needed -

function update(t, func) {
  switch (t?.constructor) {
    case Object:
      return Object.fromEntries(
        Object.entries(t).map(([k,v]) =>
          [k, func([k, update(v, func)])]
        )
      )
    case Array:
      return t.map((v, k) => func([k, update(v, func)]))
    default:
      return func([null, t])
  }
}

const schema = {
  social: {
    facebook: 'someValue',
    twitter: {
      department: {
        departmentImage: {
            editable: 'someValue'
        }
      },
      someArr: [{ editable: 1 }, { editable: 2 }, { hello: "world" }]
    },
  }
}

console.log(update(schema, ([k,v]) =>
  k == "editable" ? "✅" : v
))
.as-console-wrapper {min-height: 100% !important; top: 0}

{
  "social": {
    "facebook": "someValue",
    "twitter": {
      "department": {
        "departmentImage": {
          "editable": "✅"
        }
      },
      "someArr": [
        {
          "editable": "✅"
        },
        {
          "editable": "✅"
        },
        {
          "hello": "world"
        }
      ]
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文