嵌套JSON,使用angularjs根据Jquery中的条件删除对象

时间:2016-08-24 15:23:08

标签: javascript jquery angularjs json

我在这个变量$ scope.bbTreeData中有一个JSON对象。我试图删除标志为false的对象。我能够遍历嵌套的JSON对象,但我不知道如何删除对象?有什么建议吗?

[{
  "market": "Atl",
  "subItem": [{
    "comment_id": "1",
    "user_id": "32509",
    "flag": true
  }, {
    "comment_id": "2",
    "user_id": "32510",
    "flag": false

  }]
}, {
  "market": "Chicago",
  "subItem": [{
    "comment_id": "3",
    "user_id": "32501",
    "flag": true
  }, {
    "comment_id": "4",
    "user_id": "32502",
    "flag": false

  }]
}]

$scope.bbTreeInactiveData = angular.copy($scope.bbTreeData);
angular.forEach($scope.bbTreeInactiveData, function(item) {
  angular.forEach(item.subItem, function(record, index) {
    if (record.flag == false) {
      console.log(item.subItem, index);
      /* code to remove the object*/
    }
  });
});

3 个答案:

答案 0 :(得分:2)

您可以使用_underscorejs

_without()功能

请参阅documentation

  

<强>没有
  _.without(array, values)

     

返回数组的副本,其中删除了所有值的实例。

     

_。没有([1,2,1,0,3,1,4],0,1);
  =&GT; [2,3,4]

<强>输入

[
    {
        "market": "Atl",
        "subItem": [
            {
                "comment_id": "1",
                "user_id": "32509",
                "flag": true
            },
            {
                "comment_id": "2",
                "user_id": "32510",
                "flag": false
            }
        ]
    },
    {
        "market": "Chicago",
        "subItem": [
            {
                "comment_id": "3",
                "user_id": "32501",
                "flag": true
            },
            {
                "comment_id": "4",
                "user_id": "32502",
                "flag": false
            }
        ]
    }
]

<强>输出

[
    {
        "market": "Atl",
        "subItem": [
            {
                "comment_id": "1",
                "user_id": "32509",
                "flag": true
            }
        ]
    },
    {
        "market": "Chicago",
        "subItem": [
            {
                "comment_id": "3",
                "user_id": "32501",
                "flag": true
            }
        ]
    }
]

代码段

&#13;
&#13;
var json = JSON.parse('[{"market":"Atl","subItem":[{"comment_id":"1","user_id":"32509","flag":true},{"comment_id":"2","user_id":"32510","flag":false}]},{"market":"Chicago","subItem":[{"comment_id":"3","user_id":"32501","flag":true},{"comment_id":"4","user_id":"32502","flag":false}]}]');

for(var i=0; i<json.length; i++) {
    json[i].subItem = _.without(json[i].subItem, _.findWhere(json[i].subItem, {flag: false}));
};

console.log(JSON.stringify(json, 0, 8));
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

试试这个:

$scope.bbTreeInactiveData = angular.copy($scope.bbTreeData);

var results = $scope.bbTreeInactiveData.map(function(row) {
  return row.subItem.filter(function(cell) {
    return cell.flag == true
  });
});

使用map()filter()函数。

答案 2 :(得分:0)

您可以在纯JavaScript中使用delete关键字:

delete item.subItem[index]

我相信已有答案:How do I remove a property from a JavaScript object?

如果要删除根,请将索引参数添加到第一个forEach,然后使用数组拼接功能删除根:

$scope.bbTreeInactiveData.splice(indexRoot,1);