Array method in javascript

Array method in javascript

What is an Array?

An array is a non-primitive or reference data type that stores the multiple values of different data types in javascript. An Array is a type of object.

let EmptyArr=[]; // Empty Array Using Literal Array

// string elements
const arr = ["Java", "python", "Ruby", "javascript", "kotlin", "PHP"];  

//Array contains multiple elements of same data type i.e Number
const arr1 = [2, 4, 6, 8, 10];

// Here our array contains multiple elements of different data types 
let info=["yash",12345, false]

//object elements
const arr2 = [{ 'name': 'stark' },{ 'name': 'scarlet' }];

//nested arrays
const arr3 = [
      [1, 2, 3],
      [4, 5, 6],
      [7, 8, 9],
    ];

Array Methods

Array methods work like a function in JavaScript that we can apply to our arrays. Each method has a specific function that performs a change or calculation to our array. These methods save our time in writing the whole function for different-different requirements and also helps in avoiding complexities. Every Method has its own functionality according to array task work it is a predefined function by Js.

Array.indexof()

Array.indexof() is used to find the first occurrence index of the passed value in the method

const arr = ["Java", "python", "Ruby", "javascript", "kotlin", "PHP"];

console.log(arr.indexOf('python'))        // prints  1
console.log(arr.lastIndexOf(''))    // prints  3

Array.lastIndexof()

lastIndexof() is used to find the last occurrence index of the passed value in the method

const arr = ["Java", "python", "Ruby", "javascript", "javascript", "PHP"];

console.log(arr.lastIndexOf('javascript'))    // prints  4

Array.push()

push() is used to add new elements to arrays.

const arr = ["Java", "python", "Ruby", "javascript", "javascript"];
arr.push('PHP')
console.log(arr)  
//  prints [ "Java", "python", "Ruby", "javascript", "javascript", "PHP" ]

Array.pop()

pop() will delete the last element of the arrays.

const arr = ["Java", "python", "Ruby", "javascript", "javascript", "PHP"];
arr.pop()
console.log(arr)
// prints [ "Java", "python", "Ruby", "javascript", "javascript" ]

Array.shift()

shift() is used to remove the first index element of the array and shift all the remaining elements to 1 step before and return the removed element

const arr = ["Java", "python", "Ruby", "javascript", "javascript", "PHP"];
console.log(arr.shift()) // prints "Java"
console.log(arr)         // prints ["python", "Ruby", "javascript", "PHP" ]

Array.unshift()

unshift is used to add a new element at the beginning of the array

const arr = ["Java", "python", "Ruby", "javascript", "javascript"];
console.log(arr.unshift("PHP")) // prints 5
console.log(arr)   // [ "PHP", "Java", "python", "Ruby", "javascript", "javascript" ]

Array.slice()

slice() is used to slice out the elements in the array this method takes indexes as the arguments it can take a maximum of two arguments to start and end indexes this method doesn't modify the original array rather creates a new array and returns it will new values of the array

const arr = ["Java", "python", "Ruby","javascript"];
console.log(arr.slice(1,3))    // prints [ "python", "Ruby" ]
console.log(arr) // prints [ "Java", "python", "Ruby","javascript" ]

Array.splice()

splice() is used to add new items to the arrays this can take four arguments

const arr1 = ["Java", "python", "Ruby","javascript"];
arr1.splice(2, 0, 'php','swift')
//  2 is the start
//  0 is the number of elements to be deleted 
//  remaining arguments are to be added into the new array 
console.log(arr1) 
//prints ["Java", "python",'php','swift',"Ruby","javascript"]

Array.sort()

sort() this function is used to sort the array in alphabetical order this returns a sorted array

const arr = ['apple', 'ball', 'cat', 'dog']
console.log(arr.reverse()); // prints [ 'dog', 'cat', 'ball', 'apple' ]
console.log(arr);           // prints [ 'dog', 'cat', 'ball', 'apple' ]

Array.reverse()

reverse() this function is used to reverse array elements this returns reversed array

const arr = ['ball', 'apple', 'dog','cat']
console.log(arr.sort());    // prints [ 'apple', 'ball', 'cat', 'dog' ]
console.log(arr);           // prints [ 'ball', 'apple', 'dog','cat' ]

Array.split()

split() this method is used to convert a given string into an array it takes the argument by which the string should be split to convert it

const arr = "apple,ball,cat,dog";
const str = arr.split(',');
console.log(str);    //  [ 'apple', 'ball', 'cat', 'dog' ]

Array.join()

join() this method is used to convert the given array into a string and it takes the argument by which the array elements should be joined as a string

const arr1 = ["apple", "ball", "cat", "dog"];
const arr2 = arr1.join(',');
console.log(arr2)    //  apple,ball,cat,dog

Array.map()

map() this method is used to traverse each element of the array and to modify it based on the function passed to map()

const arr = [2,4,6,8,10]
const arr1 = arr.map(a => a*a)
console.log(arr1)    // [ 4, 16, 36, 64, 100 ]

Array.filter()

filter() this method is used to filter down the elements of the array based on certain conditions which are passed as the function to the method. If the element satisfies the condition then the element is added to the new array else it won't be added to the array

const arr2 = [1,2,3,4,5,6,7,8,9,10]
const arr3 = arr2.filter( a => a%2==0)
console.log(arr3)    //  [ 2, 4, 6, 8, 10 ]