Most used Array Methods in JS

Most used Array Methods in JS

ยท

3 min read

Hello Everyone ๐Ÿ‘‹ ,

I will be sharing the list of most used array methods in JS that are used every now and then in Array data manipulation .

Lets's Start !!

1. toString()

The first method in the list is toString() , it is used to list down all the elements in the array as comma seperated string.

let arrayOfIntegers =[1,2,3,4,5] ;
console.log(arrayOfIntegers.toString())  // 1,2,3,4,5

2. sort()

The second method in the list is sort() , using which we can sort the array elements but the sorting is done alphabetical manner . That means "2" >"1" , "a" > "A" and also "2" >"100". Let's understand this with an example below :

let arrayOfIntegers = [1,2,3,4,5,100,20] ;
arrayOfIntegers.sort();
console.log(arrayOfIntegers);  // [1,100,2,20,3,4,5]

To solve this problem of sorting for integers , JS has compare function's to sort them correctly. What compare function does for us is , it will compare the values passed inside the function and return a value based on which the data is sorted . e.g., if a> b then , a is placed after b , if a<b then a is placed before b , else they are equal.

/* To solve this problem of sorting for integers , JS<br> has compare function's to sort them correctly*/

let arrayOfIntegers =[1,2,3,4,5,100,20] ;
arrayOfIntegers.sort(function(a,b) {return a-b}); 

console.log(arrayOfIntegers)     // [1,2,3,4,5,20,100]

/* To sort in descending order */

arrayOfIntegers.sort(function(a,b) {return b-a}); 

console.log(arrayOfIntegers)     // [100,20,5,4,3,2,1]

3. join()

Moving forward , we have join() which will return the array elements as string joined with the value provided as parameter in join(). This method does not change the original array , instead return a string value.

let arr = ["happy" , "coding"];
console.log(arr.join("-"));     // happy-coding

let value = arr.join("*");
console.log(typeof value);     // string

4. from()

This method is used to convert the object to the Array but with one condition , ie., the object to be converted must have the length property associated to it . e.g., strings.

let str="ABC";
let arr = Array.from(str);
console.log(arr);  // ['A' , 'B' , 'C']

5. reverse()

This method is used to print the array elements in reverse order ,i.e., elements are printed from rigth to left .

let arr=[1,5,8,9,12,3,7];
arr.reverse();
console.log(arr);     // [ 7, 3, 12, 9, 8, 5,  1]

6. splice()

This method is used to add elements in an array at the specified positon . Synatx :- splice(start_index , count of elements to remove , elements to be added ) . More than one elements can also be added by providing comma seperated values as last param's.

let arr = [1,10,20,30];
arr.splice(2,0,5);
console.log(arr);     // [1,10,5,20,30]

arr.splice(1,0,3,4);
console.log(arr);     // [1,3,4,10,5,20,30]

/*The elements are inserted at specified index (param1) in an array and deletes the no. of elements (param2) from array*/ 

arr.splice(5,2,6,7);
console.log(arr)     //  [1,3,4,10,5,6,7]

Splice can also be used to only remove elements from an array instead of adding them to array.

/* taking advantage of the fact that third parameter in
 splice is optional , we can remove elements from specified index*/

let arr=[1,3,4,10,5,6,7] ;
arr.splice(1,2);
console.log(arr)       //  [ 1, 10, 5, 6, 7 ]  

/*removes 2 elements starting from index 1 */

7. slice()

This method is used to extract the array elements from the array . This methods returns the sliced array from original array . Syntax :- slice(start_index,last_index) , where start_index is inclusive and last_index is exclusive.

let arr = [1,5,10,15,20];
let newArr= arr.slice(1,3);
console.log(arr);                  // [1,5,10,15,20]
console.log(newArr);              //  [5,10]

I hope this article will help you understand the basic operations performend on arrays using the above methods.

If you liked this article, please be sure to โค it. Thanks in Advance๐Ÿ˜Š.

Happy Coding!

ย