我如何从Angularjs中的数组中删除某个对象

时间:2018-09-29 11:00:13

标签: javascript angularjs

我有复选框树。部门和每个部门下都有一些用于员工的复选框。当用户检查部门时,将在此部门下选择所有员工。我将唯一的部门键添加到数组中。 我的问题是如何从阵列中删除未经检查的部门密钥。

$scope.leftdept = function (m) {

        console.log(m);

        for (i = 0; i < m.length; i++) {
            if ($scope.depts.indexOf(m[i].Dep_key) === -1) {
                $scope.depts.push(m[i].Dep_key);
            }

       console.log($scope.depts);
    }

1 个答案:

答案 0 :(得分:1)

如果您只想删除对象的键,则可以这样操作:

m.forEach(function (dept) {

if(condition) // this is where you check if this department is checked or unchecked
    delete dept[Dep_key];
});

当然,这只是一个模糊的示例,我将需要知道您的实际对象定义才能为您提供正确的答案。

相关问题