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

Nested JSON, remove object based on a condition in Jquery using angularjs

本文关键字:条件 删除 对象 Jquery JSON 使用 angularjs 嵌套      更新时间:2023-09-26

我有一个JSON对象在这个变量$scope.bbTreeData。我正试图删除标记为假的对象。我能够遍历嵌套的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*/
    }
  });
});

可以使用_underscorejs的_without()函数

参见文档

,
_。没有(数组,值)

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

_。无([1,2,1,0,3,1,4],0,1);
=> [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
            }
        ]
    }
]
<<p> 代码片段/strong>

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));
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

试试这个:

$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()函数

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

delete item.subItem[index]

我相信已经有一个答案:我如何从JavaScript对象中删除属性?

如果要删除根,请在第一个forEach中添加索引参数,然后使用数组拼接函数删除根:

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