简体   繁体   中英

Update javascript object conditionally

In my Angular component, I am defining a Javascript object such as

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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