remove(function(item) { condition }) 方法
描述
KnockoutJS Observable remove(function(item) { return condition }) 方法删除满足条件的项目并将其作为数组返回。
语法
arrayName.remove(function(item) { return condition })
参数
此方法接受具有条件的函数形式的参数。数组中满足上述条件的项目将被删除。
示例
<!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>
输出
让我们执行以下步骤来查看上述代码的工作原理 −
将上述代码保存在 array-remove-fun.htm 文件中。
在浏览器中打开此 HTML 文件。
单击"按条件删除"按钮。

knockoutjs_observables.html