Array Object In TypeScript: Part 6
Before reading this article, please go through the following articles:
-
-
-
-
-
Introduction
In TypeScript, the array object makes our work easier. The TypeScript Array object stores multiple values in a single variable at a time. TypeScript provides many methods.
In this article, I am describing some of the TypeScript array methods splice and toString.
Splice() Method
In TypeScript the splice() method removes selected elements from an array and replaces them with new elements. A simple splice method without elements_list parameter removes the number of elements given by the second parameter starting with the index given by the first parameter. A splice method with an elements_list parameter removes the number of elements given by the second parameter starting with the index given by the first parameter then replaces those elements with the ones given by the third parameter. Both methods return the removed elements.
Syntax
- array.splice(start,number,elements_list)
start is a required parameter. It is a numeric value. It specifies where the function will start removing elements.
number is an optional parameter. It determines how many elements will be removed, and also is the length of the array returned.
Elements_list is an optional parameter. It specifies an array with the elements that will be inserted into the original array.
Function
- Splice() {
- var fstarry: string[] = ['C', 'Sharp', 'Corner', 'Dot', 'Net', 'Heaven', 'Modeling', 'Corner'];
- var removeelemet = fstarry.splice(2, 1, 'Nitin').toString();
- var span = document.createElement("span");
- span.innerText = "Splice Method \n Remove element is -> " + removeelemet + "\n" + "After add element, Array is -> " + fstarry + "\n";
- document.body.appendChild(span);
- }
toString() Method
In TypeScript the toString() method is used to convert an array to a string and return the string. The toString() method is the same as the join method without any parameters passed to it.
Syntax
Function
- toString() {
- var arrayString: string[] = ['C', 'Sharp', 'Corner', 'VB', 'Net', 'Heaven'];
- arrayString.toString();
- var span = document.createElement("span");
- span.style.color = "blueviolet";
- span.innerText = "toString Method \n Array is -> " + arrayString + "\n";
- document.body.appendChild(span);
- }
Complete Program
Splice_toString.ts
- class Splice_toString {
- Splice() {
- var fstarry: string[] = ['C', 'Sharp', 'Corner', 'Dot', 'Net', 'Heaven', 'Modeling', 'Corner'];
- var removeelemet = fstarry.splice(2, 1, 'Nitin').toString();
- var span = document.createElement("span");
- span.innerText = "Splice Method \n Remove element is -> " + removeelemet + "\n" + "After add element, Array is -> " + fstarry + "\n";
- document.body.appendChild(span);
- }
- toString() {
- var arrayString: string[] = ['C', 'Sharp', 'Corner', 'VB', 'Net', 'Heaven'];
- arrayString.toString();
- var span = document.createElement("span");
- span.style.color = "blueviolet";
- span.innerText = "toString Method \n Array is -> " + arrayString + "\n";
- document.body.appendChild(span);
- }
- }
- window.onload = () => {
- var obj = new Splice_toString();
- obj.Splice();
- obj.toString();
- };
Splice_toString_MethodDemo.htm
- <!DOCTYPE html>
- <html lang="en"
- xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta charset="utf-8" />
- <title>TypeScript HTML App</title>
- <link rel="stylesheet" href="app.css" type="text/css" />
- <script src="Splice_toString.js"></script>
- </head>
- <body>
- <h3>Splice() and toString() Array Method In TypeScript</h3>
- </body>
- </html>
Splice_toString.js
- var Splice_toString = (function() {
- function Splice_toString() {}
- Splice_toString.prototype.Splice = function() {
- var fstarry = ['C', 'Sharp', 'Corner', 'Dot', 'Net', 'Heaven', 'Modeling', 'Corner'];
- var removeelemet = fstarry.splice(2, 1, 'Nitin').toString();
- var span = document.createElement("span");
- span.innerText = "Splice Method \n Remove element is -> " + removeelemet + "\n" + "After add element, Array is -> " + fstarry + "\n";
- document.body.appendChild(span);
- };
- Splice_toString.prototype.toString = function() {
- var arrayString = ['C', 'Sharp', 'Corner', 'VB', 'Net', 'Heaven'];
- arrayString.toString();
- var span = document.createElement("span");
- span.style.color = "blueviolet";
- span.innerText = "toString Method \n Array is -> " + arrayString + "\n";
- document.body.appendChild(span);
- };
- return Splice_toString;
- })();
- window.onload = function() {
- var obj = new Splice_toString();
- obj.Splice();
- obj.toString();
- };