简体   繁体   English

如何修改嵌套 object 的属性?

[英]how to modify properties of an nested object?

I have the following nested object and I need to leave the "alias" property blank and the "group" property set to true for all "entries" and "exits".我有以下嵌套的 object,我需要将“别名”属性留空,并将所有“条目”和“退出”的“组”属性设置为 true。 I also need to delete the whole "parameters" object.我还需要删除整个“参数”object。 Would there be a way to do it all in one function?有没有办法在一个 function 中完成这一切? I've tried to apply the delete Object method but it doesn't work as it's an indexed object.我尝试应用删除 Object 方法,但它不起作用,因为它是索引 object。

{
  "1": {
    "x": 114,
    "y": 135,
    "properties": {
      "id": 1,
      "entries": {
        "entry_0": {
          "id": 1,
          "alias": "do",
          "group": false
        }
      },
      "exits": {
        "exit_0": {
          "id": 1,
          "alias": "re",
          "group": false
        }
      },
      "parameters": {
        "parameter_0": {
          "id": 3,
          "group": false
        }
      },
      "order": 1
    }
  },
  "2": {
    "x": 700,
    "y": 104,
    "properties": {
      "id": 1
      "entries": {
        "entry_0": {
          "id": 1
          "alias": "do"
          "group": false
        }
      },
      "exits": {
        "exyt_0": {
          "id": 1
          "alias": "re"
          "group": false
        }
      },
      "parameters": {
        "parameter_0": {
          "id": 3
          "alias": "mi"
          "group": false
        }
      },
      "order": 2
    }
  }
}

the desired nested object would be the following所需的嵌套 object 将如下

{
  "1": {
    "x": 114,
    "y": 135,
    "properties": {
      "id": 1,
      "entries": {
        "entry_0": {
          "id": 1,
          "alias": "",
          "group": true
        }
      },
      "exits": {
        "exit_0": {
          "id": 1,
          "alias": "",
          "group": true
        }
      },
      "order": 1
    }
  },
  "2": {
    "x": 700,
    "y": 104,
    "properties": {
      "id": 1
      "entries": {
        "entry_0": {
          "id": 1
          "alias": ""
          "group": true
        }
      },
      "exits": {
        "exyt_0": {
          "id": 1
          "alias": ""
          "group": true
        }
      },
      "order": 2
    }
  }
}

what I've tried is the following, managing to delete the "parameters" object but I can't access the "label" property of each "entry" and "exit我尝试过的是以下,设法删除“参数” object 但我无法访问每个“条目”和“退出”的“标签”属性

const nedtedObjectsValues = Object.values(nestedObjects);
for (object of nedtedObjectsValues) {
   delete object.properties.parameters;

}

if anyone can give me an idea of how to approach this function.如果有人可以告诉我如何处理这个 function。 Thank you in advance.先感谢您。

In JavaScript, to reference numeric object properties, you need to use the square brackets syntax:在 JavaScript 中,要引用数字 object 属性,您需要使用方括号语法:

object.1 // bad
object[1] // good

You can delete numeric property like this:您可以像这样删除数字属性:

delete object[1];

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM