简体   繁体   English

有条件地更新javascript对象

[英]Update javascript object conditionally

In my Angular component, I am defining a Javascript object such as 在Angular组件中,我正在定义Javascript对象,例如

myObj = {
        "option": ["valid"],
        "cities": [
          "London",
          "Paris",
          "Rome"
        ],
        "routes": [{
          "section": 'option1',
          "class": true
        }],
        "def": [
          {
            "name": "name1",
            "attributes": [
              "test"
            ]
          }
        ]
      }

Now based on a condition, I want to either include or exclude the 'routes' section. 现在根据条件,我想包括或排除“路线”部分。

So, currently I am doing like this 所以,目前我正在这样

if(somecondition) {
   myObj = {
        "option": ["valid"],
        "cities": [
          "London",
          "Paris",
          "Rome"
        ],
        "routes": [{
          "section": 'option1',
          "class": true
        }],
        "def": [
          {
            "name": "name1",
            "attributes": [
              "test"
            ]
          }
        ]
      }
} else {
   myObj = {
        "option": ["valid"],
        "cities": [
          "London",
          "Paris",
          "Rome"
        ],
        "def": [
          {
            "name": "name1",
            "attributes": [
              "test"
            ]
          }
        ]
      }
}     

Wanted to check if there is a better way to achieve this as this is duplication of code and as the conditions increase this would no longer be maintainable. 想要检查是否有更好的方法来实现此目的,因为这是重复的代码,并且随着条件的增加,此方法将不再可维护。

You can delete a property of an object that way : 您可以通过以下方式删除对象的属性:

delete myObj.routes;

or 要么

delete myObj['routes'];

Is that what you are looking for ? 那是您要找的东西吗?

Try the following instead: 请尝试以下操作:

myObj = {
        "option": ["valid"],
        "cities": [
          "London",
          "Paris",
          "Rome"
        ],
        "def": [
          {
            "name": "name1",
            "attributes": [
              "test"
            ]
          }
        ]
      }

if(somecondition) {
   myObj.routes = [{
          "section": 'option1',
          "class": true
        }]
}   

Define the object before and add the routes based on the condition. 之前定义对象,然后根据条件添加routes

You can try below code. 您可以尝试以下代码。 Here if condition is not satisfied you can simply remove routes from object. 如果不满足条件,则可以从对象中删除路由。 Hope it help! 希望对您有所帮助!

 myObj = {
            "option": ["valid"],
            "cities": [
              "London",
              "Paris",
              "Rome"
            ],
            "routes": [{
              "section": 'option1',
              "class": true
            }],
            "def": [
              {
                "name": "name1",
                "attributes": [
                  "test"
                ]
              }
            ]
          };

        if(!somecondition) {
          delete myObj.routes;
        }

By simply adding the routes property later based on your condition 通过稍后根据您的条件简单地添加routes属性

myObj = {
  "option": ["valid"],
  "cities": [
    "London",
    "Paris",
    "Rome"
  ],
  "def": [{
    "name": "name1",
    "attributes": [
      "test"
    ]
  }]
};

if(somecondition) {
   myObj.routes = [{
       "section": 'option1',
       "class": true
   }];
}

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

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