【问题标题】:How to Iterate through all property of object and delete specific property AngularJS如何遍历对象的所有属性并删除特定属性AngularJS
【发布时间】:2017-12-05 22:51:41
【问题描述】:

我有一个 JavaScript 对象,如下所示:

{
    "widget": {
        "debug": "on",
        "test": {},
        "window": [{
            "title": "Sample Konfabulator Widget",
            "name": "main_window",
            "width": 500,
            "height": 500,
            "test": {}
        }]
        "image": {
            "src": "Images/Sun.png",
            "name": "sun1",
            "test": {},
            "vOffset": 250
        },
        "text": {
            "data": "Click Here",
            "test": {},
            "alignment": "center",
            "image": {
                "src": "Images/test.png",
                "name": "sun2",
                "test": {}
            }
        }
    }
}

现在我想遍历对象并删除所有"test" 属性。我该怎么做?

【问题讨论】:

    标签: javascript angularjs json loops


    【解决方案1】:

    var data = {
      "widget": {
        "debug": "on",
        "test": {},
        "window": [{
          "title": "Sample Konfabulator Widget",
          "name": "main_window",
          "width": 500,
          "height": 500,
          "test": {}
        }],
        "image": {
          "src": "Images/Sun.png",
          "name": "sun1",
          "test": {},
          "vOffset": 250
        },
        "text": {
          "data": "Click Here",
          "test": {},
          "alignment": "center",
          "image": {
            "src": "Images/test.png",
            "name": "sun2",
            "test": {}
          }
        }
      }
    };
    
    function DeleteProperty(input, name) {
      if (input instanceof Object) {
        for (var prop in input) {
          if (prop == name)
            delete input[prop]
          else
            DeleteProperty(input[prop], name)
        }
      }
    }
    
    DeleteProperty(data, 'test');
    console.log(data);

    【讨论】:

      猜你喜欢
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 2012-01-08
      相关资源
      最近更新 更多