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

标签 javascript jquery angularjs json

我在这个变量 $scope.bbTreeData 中有一个 JSON 对象。我正在尝试删除标志为假的对象。我能够遍历嵌套的 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() 函数

参见 documentation

without
_.without(array, values)

Returns a copy of the array with all instances of the values removed.

_.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
            }
        ]
    }
]

代码片段

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>

关于javascript - 嵌套的 JSON,使用 angularjs 根据 Jquery 中的条件删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39127325/

相关文章:

javascript - 幻灯片使用文字代替图片的几个问题

javascript - 过渡动画在 Angular 中不起作用

angularjs - Angular UI 选择 : How to add a tooltip for text overflow?

javascript - Jquery函数中的angularJS : Uncaught TypeError: Cannot read property 'noDisagree' of undefined

javascript - ajax/javascript 在 ie 中不起作用

javascript - 如何通过javascript为标签赋值?

javascript - TinyMCE 忽略提交数据的初始化选项

javascript - 处理 javascript db.transaction 回调

jquery - 如何使用 jQuery 将动态数据传递到 Google Analytics 事件跟踪脚本

jquery tokeninput 过滤器查询发送额外参数