Array Object In TypeScript: Part 2
Before reading this article, please go through the following articles:
Introduction
In this article, I am describing the TypeScript array methods Join and lastIndexOf.
Join() Method
Syntax
- array.join(separator)
Function
- Join() {
- var fstarry: string[] = ['C', 'Sharp', 'Corner', '.com'];
- var result = fstarry.join();
- var result1 = fstarry.join('+');
- var result2 = fstarry.join('*');
- var span = document.createElement("span");
- span.innerText = "Join Method \n First array Sample join as simple -> " + result + "\n" + "Second array sample join with plus (+) -> " + result1 + "\n" + "Third array sample join with (*) -> " + result2 + "\n";
- document.body.appendChild(span);
- }
LastIndexOf() Method
In TypeScript the lastIndexOf() method searches for the last occurrence of a string value from the given characters. In the lastIndexOf() method the searchvalue parameter will start from a specified position.
If the position is not set then the search will start from the beginning. The beginning position starts at 0. After searching it will return the index number of values. If it does not find a match then it will return -1. The lastIndexOf() method is case sensitive.
Syntax
- array.lastIndexOf(searchvalue,start)
- LastIndexOf() {
- var arrayName: string[] = ['C', 'Sharp', 'Corner', 'VB', 'Net', 'Heaven'];
- var index = arrayName.lastIndexOf('Corner');
- var span = document.createElement("span");
- span.style.color = "#7a1111";
- span.innerText = "LastIndexOf Method \n LastIndexOf of (Corner) in array is -> " + index;
- document.body.appendChild(span);
- }
Complete Program
Join_LastIndexOf.ts
- class Join_LastIndexOf
- {
- Join()
- {
- var fstarry: string[] = ['C', 'Sharp', 'Corner', '.com'];
- var result = fstarry.join();
- var result1 = fstarry.join('+');
- var result2 = fstarry.join('*');
- var span = document.createElement("span");
- span.innerText = "Join Method \n First array Sample join as simple -> " + result + "\n" + "Second array sample join with plus (+) -> " + result1 + "\n" + "Third array sample join with (*) -> " + result2 + "\n";
- document.body.appendChild(span);
- }
- LastIndexOf()
- {
- var arrayName: string[] = ['C', 'Sharp', 'Corner', 'VB', 'Net', 'Heaven'];
- var index = arrayName.lastIndexOf('Corner');
- var span = document.createElement("span");
- span.style.color = "#7a1111";
- span.innerText = "LastIndexOf Method \n LastIndexOf of (Corner) in array is -> " + index;
- document.body.appendChild(span);
- }
- }
- window.onload = () =>
- {
- var obj = new Join_LastIndexOf();
- obj.Join();
- obj.LastIndexOf();
- };
Join_LastIndexOf_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="Join_LastIndexOf.js"></script>
- </head>
- <body>
- <h3>Join() and LastIndexOf() Array Method In TypeScript</h3>
- </body>
- </html>
Join_LastIndexOf.js
- var Join_LastIndexOf = (function() {
- function Join_LastIndexOf() {}
- Join_LastIndexOf.prototype.Join = function() {
- var fstarry = [
- 'C',
- 'Sharp',
- 'Corner',
- '.com'
- ];
- var result = fstarry.join();
- var result1 = fstarry.join('+');
- var result2 = fstarry.join('*');
- var span = document.createElement("span");
- span.innerText = "Join Method \n First array Sample join as simple -> " + result + "\n" + "Second array sample join with plus (+) -> " + result1 + "\n" +
- "Third array sample join with (*) -> " + result2 + "\n";
- document.body.appendChild(span);
- };
- Join_LastIndexOf.prototype.LastIndexOf = function() {
- var arrayName = [
- 'C',
- 'Sharp',
- 'Corner',
- 'VB',
- 'Net',
- 'Heaven'
- ];
- var index = arrayName.lastIndexOf('Corner');
- var span = document.createElement("span");
- span.style.color = "#7a1111";
- span.innerText = "LastIndexOf Method \n LastIndexOf of (Dot) in array is -> " + index;
- document.body.appendChild(span);
- };
- return Join_LastIndexOf;
- })();
- window.onload = function() {
- var obj = new Join_LastIndexOf();
- obj.Join();
- obj.LastIndexOf();
- };
- //@ sourceMappingURL=Join_LastIndexOf.js.map
Output


Comments
Join the conversation! Your thoughts help the community grow.