目录

remove(function(item) { condition })

描述 (Description)

KnockoutJS Observable remove(function(item) { return condition })方法删除满足条件的项并将它们作为数组返回。

语法 (Syntax)

arrayName.remove(function(item) { return condition })

参数 (Parameters)

该方法接受具有条件的函数形式的参数。 满足上述条件的数组中的项目将被删除。

例子 (Example)

<!DOCTYPE html>
   <head>
      <title>KnockoutJS ObservableArray function based remove method</title>
      <script src = "https://ajax.aspnetcdn.com/ajax/knockout/knockout-3.1.0.js"
         type = "text/javascript"></script>
   </head>
   <body>
      <p>Example to demonstrate function based remove() method.</p>
      <button data-bind = "click: removeoncondEmp">Remove on condition</button>
      <p>Array of employees: <span data-bind = "text: empArray()" ></span></p>
      <script>
         function EmployeeModel() {
            this.empName = ko.observable("");
            this.chosenItem = ko.observableArray("");
            this.empArray = ko.observableArray(['Scott','James','Jordan','Lee',
               'RoseMary','Kathie']);
            this.removeoncondEmp = function() {
               alert("This function is removing items whos value is 'James'.");
               this.empArray.remove(function (empName) { return empName === 'James' });  
                  //remove where empName is James
            }
         }
         var em = new EmployeeModel();
         ko.applyBindings(em);
      </script>
   </body>
</html>

输出 (Output)

让我们执行以下步骤来查看上述代码的工作原理 -

  • 将以上代码保存在array-remove-fun.htm文件中。

  • 在浏览器中打开此HTML文件。

  • 单击“删除条件”按钮。

新页面打开
↑回到顶部↑
WIKI教程 @2018