Introduction
In TypeScript, the array object makes work easier. The TypeScript Array object is used to store 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.
Concat() Method
In TypeScript the concat() method is used to concatenate or combine two or more arrays and return a new array. It does not change the existing arrays, it returns a new array, which contain the values of both arrays.
Syntax
array.concat(value1,value2,valu3,....................valuen) |
array: The Array object for which all other arrays are concatenated.
value: The values are array items to add to the end of the array.
Function
Concat()
{
var fstarry: string[] = ['C','Sharp'];
var scndarry: string[] = ['Corner', '.com'];
var result = fstarry.concat(scndarry);
var span = document.createElement("span");
span.innerText = "Concat Method \n First Array is -> " + fstarry + "\n" + "Second Array is -> " + scndarry + "\n" + "Concatenated Array is -> " + result+"\n";
document.body.appendChild(span);
}
IndexOf() Method
In TypeScript the indexOf() method is used to search for a specified value's index number from the array. In the indexOf() method, the searchvalue parameter will a specific position to start from.
If the position is not set then the search will start from the beginning. The begining position starts from 0. After the search it will return an index number of values; if there is not a match then it will return -1.
Syntax
array.indexOf(searchvalue,start) |
Function
IndexOf()
{
var arrayName: string[] = ['C','Sharp','Corner','Dot','Net','Heaven'];
var index = arrayName.indexOf('Dot');
var span = document.createElement("span");
span.style.color = "green";
span.innerText = "IndexOf Method \n Index of (Dot) in array is -> " + index;
document.body.appendChild(span);
}
Complete Program
Concat_IndexOf.ts
class Concat_IndexOf
{
Concat()
{
var fstarry: string[] = ['C','Sharp'];
var scndarry: string[] = ['Corner', '.com'];
var result = fstarry.concat(scndarry);
var span = document.createElement("span");
span.innerText = "Concat Method \n First Array is -> " + fstarry + "\n" + "Second Array is -> " + scndarry + "\n" + "Concatenated Array is -> " + result+"\n";
document.body.appendChild(span);
}
IndexOf()
{
var arrayName: string[] = ['C','Sharp','Corner','Dot','Net','Heaven'];
var index = arrayName.indexOf('Dot');
var span = document.createElement("span");
span.style.color = "green";
span.innerText = "IndexOf Method \n Index of (Dot) in array is -> " + index;
document.body.appendChild(span);
}
}
window.onload = () =>
{
var obj = new Concat_IndexOf();
obj.Concat();
obj.IndexOf();
};
Concat_IndexOf_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="Concat_IndexOf.js"></script>
</head>
<body>
<h3>Concat() and IndexOf() Array Method In TypeScript</h3>
</body>
</html>
Concat_IndexOf.js
var Concat_IndexOf = (function () {
function Concat_IndexOf() { }
Concat_IndexOf.prototype.Concat = function () {
var fstarry = [
'C',
'Sharp'
];
var scndarry = [
'Corner',
'.com'
];
var result = fstarry.concat(scndarry);
var span = document.createElement("span");
span.innerText = "Concat Method \n First Array is -> " + fstarry + "\n" + "Second Array is -> " + scndarry + "\n" + "Concatenated Array is -> " + result + "\n";
document.body.appendChild(span);
};
Concat_IndexOf.prototype.IndexOf = function () {
var arrayName = [
'C',
'Sharp',
'Corner',
'Dot',
'Net',
'Heaven'
];
var index = arrayName.indexOf('Dot');
var span = document.createElement("span");
span.style.color = "green";
span.innerText = "IndexOf Method \n Index of (Dot) in array is -> " + index;
document.body.appendChild(span);
};
return Concat_IndexOf;
})();
window.onload = function () {
var obj = new Concat_IndexOf();
obj.Concat();
obj.IndexOf();
};
//@ sourceMappingURL=Concat_IndexOf.js.map
Output