
- KnockoutJS Tutorial
- KnockoutJS - Home
- KnockoutJS - Overview
- KnockoutJS - Environment Setup
- KnockoutJS - Application
- KnockoutJS - MVVM Framework
- KnockoutJS - Observables
- Computed Observables
- KnockoutJS - Declarative Bindings
- KnockoutJS - Dependency Tracking
- KnockoutJS - Templating
- KnockoutJS - Components
- KnockoutJS Resources
- KnockoutJS - Quick Guide
- KnockoutJS - Resources
- KnockoutJS - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
KnockoutJS - splice() Method
Description
The KnockoutJS Observable splice() method takes 2 parameters specifying the startindex and the end-index. It removes items starting from start to end index and returns them as an array.
Syntax
arrayName.splice(start-index,end-index)
Parameters
Accepts 2 parameters, start-index is start index and end-index is end index.
Example
<!DOCTYPE html> <head> <title>KnockoutJS ObservableArray splice 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 splice() method.</p> <button data-bind = "click: spliceEmp">Splice Emp</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.spliceEmp = function() { alert("Splice is removing items from index 1 to 3(If exists)."); this.empArray.splice(1,3); // remove 2nd,3rd and 4th item, as array index //starts with 0. } } var em = new EmployeeModel(); ko.applyBindings(em); </script> </body> </html>
Output
Let's carry out the following steps to see how the above code works −
Save the above code in array-splice.htm file.
Open this HTML file in a browser.
Click the Splice Emp button and observe that items starting from index 1 to 3 are removed.
knockoutjs_observables.htm
Advertisements