Methods in Arrays – Part 1

Arrays topic in TypeScript is divided into following parts:

Below are the methods that are discussed in this section

concat every filter
forEach indexOf join
concat

This contacts two or more arrays and returns a new array.
Syntax

array1.concat(array2, array3,...);

Below is a sample code

var nums:string[] = new Array("10","20","30");
var chars:string[] = new Array('a','b','c');
var strings:string[] = new Array("Hello","Hi");
var i:number;
var newArray = strings.concat(chars, nums);
for(i=0; i<newArray.length; i++){
    console.log(newArray[i]);
}

Below is the output of above code

every

This method tests whether all array elements passes the test implemented by the function.
Returns true is all the array elements passes in the function test, otherwise false.
functionName is the name of the function used to test the array elements
Syntax

arrayName.every(functionName);

Below is a sample code

// returns true
function test1(element, index, array) { 
    return (element >= 10); 
 } 
 var nums1:number[] = [10,20,30];
 var result1 = nums1.every(test1); 
 console.log("Test Value : " + result1);

 // returns false
 function test2(element, index, array) { 
    return (element > 10); 
 } 
 var nums2:number[] = [10,5,30];
 var result2 = nums2.every(test2); 
 console.log("Test Value : " + result2 );

Below is the output of above code

filter

This function will create a new array with the elements that passes a test implemented by a function.
Returns a new array
functionName is the name of the function used to test the array elements
Syntax

arrayName.filter(functionName);

Below is a sample code

function even(element, index, array) { 
    return (element%2 == 0); 
 } 
 var nums1:number[] = [5,10,20,30,105,64];
 var result1 = nums1.filter(even); 
 console.log("Even Numbers: " + result1);

Below is the output of above code

forEach

This method calls a function for each element in the array.
Syntax

arrayName.forEach(funcionName)

Below is an example

let users = ["Akanksha","Aarav","Anita","Raj"];
users.forEach(function(value){
    console.log(value);
});

Below is the output of above code

indexOf

This method will return the first index in which the element is found. It will return -1 if the element is not found.
Returns the first index in which the element is found. It only returns one index even if the value is present at multiple indexes
value: the value which we are looking in the array
fromIndex: the index from which we want to start the search. This is optional. By default, it will start the search from the beginning of the array.
Syntax

arrayName.indexOf(value, [fromIndex[);

Below is an example

var nums:number[] = [10,20,30,20,40,50,10];
console.log("Original array: " + nums);
console.log("Search for value 20: " + nums.indexOf(20));
console.log("Search for value 20, search start from 2nd index: " + nums.indexOf(20,2));
console.log("Search for 50: " + nums.indexOf(50));

Below is the output of above code

join

This method joins all the array elements into a string. We can pass a custom separator which can be used to separate the array values in the string. By default, it will use comma (,) as a separator.
Syntax

arrayName.join(separator);

Below is an example

var users:string[] = ["Akanksha","Aarav","Apoorv","Anita","Raj"];
console.log("Original Array: " + users);
console.log(users.join());
console.log(users.join(", "));
console.log(users.join(" "));
console.log(users.join("# "));

Below is the output of above code

Strings in TypeScript – Part 1

I am dividing Strings in three parts.

  • Part 1. This is Part 1
  • Part 2
  • Part 3
  • This section has the details of following topics:

    constructor length prototype
    charAt concat indexOf

    In simple terms, Strings is a collection of characters. We can create an object of String class and perform many functions on it.
    Syntax for creating a new String object and assigning value to it.

    var variableName = new String(value);
    
    String Constructor: String()

    It is used to create an object of String type. We can also assign an initial value to that String object.
    Below is an example of creating a String object, assigning a string value to it using a constructor and fetching that value.

    var str=new String("Hello");
    console.log(str.valueOf());
    

    Below is the output of above code

    length

    Returns the length of the string. Counts the white spaces as well. This will return the number.
    Syntax

    variable.Length
    

    Below is a sample code

    var str = new String("Hello World!!");
    console.log("Length of String: " + str.length);
    

    Below is the output of above code

    charAt()

    This method will return the character at a specified index. The index starts with 0(zero) and the index of the last character is string.length – 1. Index which is out of range returns nothing, not even undefined.
    Syntax

    variable.charAt(index);
    

    Below is a sample code

    var str = new String("Hello World!!");
    console.log("1st Character: " + str.charAt(0));
    console.log("3rd character: " + str.charAt(2));
    console.log("6th Character, which is space: " + str.charAt(5));
    console.log("7th character: " + str.charAt(6));
    console.log("Index which is out of range: " + str.charAt(13));
    

    Below is the output of above code

    concat()

    This method merges two or more strings into a one single string.
    Syntax

    string1.concat(string2, string3, ....);
    

    Below is a sample code

    var var1:string = "Hello ";
    var var2:string = "Friends!! ";
    var var3:string = "Good Morning.";
    var result = var1.concat(var2);
    var result1 = var1.concat(var2,var3);
    console.log("var1 + var2: " + result);
    console.log("var1 + var2 + var3: " + result1);
    

    Below is the output of above code

    indexOf

    This method returns the index of a specified value within a string. Search will start from the starting of the string. variable is the string in which we are searching for the value. searchString is the value which we are searching in the variable. fromIndex is an integer which specify the starting index for the search. It will return -1 if the string is not found.
    Syntax

    variable.indexOf(searchString, [fromIndex])
    

    Below is an example

    var str1 = new String("This is a sample string sample");
    console.log("Index of i: " + str1.indexOf('i'));
    console.log("Index of s: " + str1.indexOf('s', 8));
    console.log("Index of is: " + str1.indexOf("is"));
    console.log("Index of sample: " + str1.indexOf("sample"));
    console.log("Index of sample: " + str1.indexOf("sample", 14));
    

    Below is the output of above code

Power Platform Academy

Start or Upgrade your Career with Power Platform

Learn with Akanksha

Python | Azure | AI/ML | OpenAI | MLOps

Design a site like this with WordPress.com
Get started