简体   繁体   English

为巨大的深度嵌套对象中的每个对象添加新的键/值

[英]add new key/value to each object inside a huge deeply nested objects

I have a huge object, with almost 50k lines.我有一个巨大的对象,几乎有 50k 行。 I need to add in each object new key with the current path of the node我需要使用节点的当前路径在每个对象中添加新键

Example:例子:


let obj = {
  "title": "RESSONÂNCIA MAGNÉTICA DA COLUNA LOMBAR",
  "data": [{
    "title": "Método:",
    "data": [{
        "title": "Procedimento 1",
        "data": [{
            "title": "CONTRASTE"
          },
          {
            "title": "CONTRASTE 2"
          }
        ]
      },
      {
        "title": "Procedimento 2",
        "data": [{
            "title": "CONTRASTE 3"
          },
          {
            "title": "CONTRASTE 4"
          }
        ]
      }
    ]
  }]
}

And I need to change my object to return this:我需要改变我的对象来返回这个:

obj = {
  "path": "$",
  "title": "RESSONÂNCIA MAGNÉTICA DA COLUNA LOMBAR",
  "data": [{
    "path": "$.data.0",
    "title": "Método:",
    "data": [{
        "path": "$.data.0.data.0",
        "title": "Procedimento 1",
        "data": [{
            "path": "$.data.0.data.0.data.0",
            "title": "CONTRASTE"
          },
          {
            "path": "$.data.0.data.0.data.1",
            "title": "CONTRASTE 2"
          }
        ]
      },
      {
        "path": "$.data.0.data.1",
        "title": "Procedimento 2",
        "data": [{
            "path": "$.data.0.data.1.data.0",
            "title": "CONTRASTE 3"
          },
          {
            "path": "$.data.0.data.1.data.1",
            "title": "CONTRASTE 4"
          }
        ]
      }
    ]
  }]
}

If you notice, i added the key path inside each object for key data , with the current path of the node.如果你注意到,我添加了关键路径中的每个对象的关键数据中,与节点的电流路径。 This is what I need.这就是我需要的。

All my object are much bigger then this example, with much more nested objects我所有的对象都比这个例子大得多,有更多的嵌套对象

 const data = { title: "RESSONÂNCIA MAGNÉTICA DA COLUNA LOMBAR", type: "template", data: [ { title: "Método:", type: "top-level-component", data: [ { title: "Procedimento", type: "navigation", normal: "CONTRASTE", checked: true, data: [ { type: "single-selection", title: "CONTRASTE", }, { type: "single-selection", title: "CONTRASTE 2", }, ], }, { title: "Procedimento 2", type: "navigation", normal: "CONTRASTE", checked: false, data: [ { type: "single-selection", title: "CONTRASTE 3", }, { type: "single-selection", title: "CONTRASTE 4", }, ], }, ], }, ], }; function addPathToObj(obj, path) { obj.path = path; for (const [key, value] of Object.entries(obj)) { if (Array.isArray(value)) { value.forEach((item, index) => { addPathToObj(item, `${path}.data.${index}`); }); } } } addPathToObj(data, "$"); console.log(data);

Performant version in case you have to deal with large files高性能版本,以防您必须处理大文件

 const data = { "title": "RESSONÂNCIA MAGNÉTICA DA COLUNA LOMBAR", "data": [{ "title": "Método:", "data": [{ "title": "Procedimento 1", "data": [{ "title": "CONTRASTE" }, { "title": "CONTRASTE 2" } ] }, { "title": "Procedimento 2", "data": [{ "title": "CONTRASTE 3" }, { "title": "CONTRASTE 4" } ] } ] }] } function PathUpdate(data, path) { data.path = path; const nd = data.data; if (nd == null) return; for (let i = 0; i < nd.length; i++) { PathUpdate(nd[i], `${path}.data.${i}`); } } console.log("before", { data }); PathUpdate(data, "$"); console.log("after", { data });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在对象数组中为每个 object 添加带有值的键? - How to add the key with value for each object inside an array of objects? 我如何为这个庞大的对象列表中的每个对象添加一个额外的键和值 - How can i add an extra key and value to each object in this huge list of objects 修改/添加深度嵌套在对象内部的数组 - Modify/Add A Array Deeply Nested Inside Object 获取深度嵌套的 object 中的键值 - Get value of the key in deeply nested object 将带有值的新键添加到嵌套对象数组中 - Add new key with Value to an array of nested Objects 根据给定的键和值在深度嵌套的对象数组中添加/更新属性 - Add/Update a property in a deeply nested array of objects based on a given key and value 迭代未知级别的深层嵌套对象,并根据用户提供的条件添加删除键/值 - Iterate deeply nested object with unknown level and add remove key/value based on user provided conditions 如果存在键名数组,则更改对象的深层嵌套键值 - Change deeply nested key value of an object if there's an array of key names 如何动态地向其他 object 内的所有嵌套 object 添加新的键/值? - How to dinamically add new key/value to all nested object inside other object? 将对象添加到深度嵌套的对象 - Add an object to a deeply nested object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM