Array and its methods

Array and its methods

Let's figure out array and its methods...

Array in Javascript:-

An Array may be defined as a type of data structure that is used for storing more the one value into a single variable. or we can say that it's used for storing a sequence of values. we can store all types of value in it. Array prevent us to create more variable for storing more than one values.

How to declare array in JS

1) JavaScript array literal

First and most recommended method is using square brackets :-

let arr1=["Aashish","Arun","Shivani","Hariom", 45 14]
console.log(arr1)

2) JavaScript Array directly (new keyword)

Another method is also well and good by using new Array() but it's not recommended

let x=new Array("Aashish","Keshu","Jitesh",85,96,751,4562)
console.log(x);

Array Methods:-

Array methods are the function that can we apply on our array to make things more easier.

1.push()

Adds elements to end of array.

Example

let ar1=["Aashish","Gopal","Hariom","Shivani","Shubham",89,33,"Rohan"]
ar1.push("ADD item")
console.log(ar1);

Output:-

[
  'Aashish',  'Gopal',
  'Hariom',   'Shivani',
  'Shubham',  89,
  33,         'Rohan',
  'ADD item'
]

2. pop()

Removes and returns the last array element

Example

let ar2 =["Aashish","Gopal","Hariom","Shivani","Shubham",89,33,"Rohan"]
ar2.pop()
console.log(ar2);

Output:-

Rohan

3.shift()

This method removes the first element of the array and shifts all other elements to a lower index.

Example

let ar1=["Aashish","Gopal","Hariom","Shivani","Shubham",89,33,"Rohan"]
ar1.shift();
console.log(ar1);

Output

[
  'Gopal',   'Hariom',
  'Shivani', 'Shubham',
  89,        33,
  'Rohan'
]

4.unshift()

This method adds one or more elements to the array starting at the beginning of the array.

Example

let ar1=["Aashish","Gopal","Hariom","Shivani","Shubham",89,33,"Rohan"]
ar1.unshift("New Element");
console.log(ar1);

Output:-

[
  'New Element', 'Aashish',
  'Gopal',       'Hariom',
  'Shivani',     'Shubham',
  89,            33,
  'Rohan'
]

5.concat()

This method is used to add two or more array

Example:-

let arr1=["Rohan","Gopal","Yogesh"]
let arr2=["Rahul","Dixit","shubham"]
let arr3=[56,88,"Shweta"]
console.log(arr1.concat(arr2,arr3));

Output:-

[
  'Rohan',  'Gopal',
  'Yogesh', 'Rahul',
  'Dixit',  'shubham',
  56,       88,
  'Shweta'
]

6. slice()

This method slices out a piece of an array into a new array. This method does not remove any elements from the source array. This method has two parameters start and end. A start has a default value of 0, thus if the start is not specified, it will take that value by default. and the second parameter define the end element if start element is 2 and last element is 5 than it return all the element from 2 to 4 . 5 will not be included.

let arr1=["Rohan","Gopal","Yogesh","Rahul","Dixit","shubham"]
console.log(arr1.slice(1,5));

Output:-

[ 'Gopal', 'Yogesh', 'Rahul', 'Dixit' ]

7. splice()

This method can be used to add new items to an array. The first parameter defines the position where new elements should be added. The second parameter defines how many elements should be removed.

Example:-

let arr1=["Rohan","Gopal","Yogesh","Rahul","Dixit","shubham"]
arr1.splice(2,1,"Aashish")
console.log(arr1);

Output:-

[ 'Rohan', 'Gopal', 'Aashish', 'Rahul', 'Dixit', 'shubham' ]

8. split()

This method automatically converts an string to a comma-separated array when a primitive value is expected and then console logs it.

Example:-

let x="Aashish Thakur";
let y=x.split('')
console.log(y);

Output:-

[
  'A', 'a', 's', 'h',
  'i', 's', 'h', ' ',
  'T', 'h', 'a', 'k',
  'u', 'r'
]

9. reverse()

This method is used to reverse the order of the array.

let arr1=["Rohan","Gopal","Yogesh","Rahul","Dixit","shubham"]
arr1.reverse();
console.log(arr1);

Output:-

[ 'shubham', 'Dixit', 'Rahul', 'Yogesh', 'Gopal', 'Rohan' ]

10.join()

This method also joins string to all array elements.

let Arr1 = [1, 2, 3, 4, 5, 6, 7];
let var1 = Arr1.join('*1*');
console.log(var1);

Output:-

1*1*2*1*3*1*4*1*5*1*6*1*7

11. sort()

This method sorts the array's elements alphabetically. Sorting means placing the components in either ascending or descending order.

Example:-

let ary1=["Rahul","Shivani","Hariom","Manish","Aashish"]
ary1.sort()
console.log(ary1);

Output:-

[ 'Aashish', 'Hariom', 'Manish', 'Rahul', 'Shivani' ]

12. includes()

This method returns a boolean value if a string contains a specified string "true" if present and "false" if not present.

Example:-

let ary1=["Rahul","Shivani","Hariom","Manish","Aashish"]
 let ary2=ary1.includes("Aashish")
console.log(ary2);

Output:-

true

13. indexOf()

This method returns the position of the first occurrence of a value in the array. If the value cannot be found, -1 is returned. This method has two parameters the item we want to search and the position from where we want to start the search in the start.

Example:-

let ary1=["Rahul","Shivani","Hariom","Aashish","Manish","Aashish","Gopal",56,"Aashish"]
 let ary2=ary1.indexOf( "Aashish")
console.log(ary2);

Output:-

3

14. lastIndexOf()

This method returns the last index of the first occurrence of a value in the array. If the value cannot be found, -1 is returned.

let ary1=["Rahul","Shivani","Hariom","Aashish","Manish","Aashish","Gopal",56,"Aashish"]
 let ary2=ary1.lastIndexOf( "Aashish")
console.log(ary2);

Output:-

8

15. isarray():-

This method returns a boolean value after checking the given element is a part of array or not.

let ary1=["Rahul","Shivani","Hariom","Aashish","Gopal",56,"Aashish"]
console.log(Array.isArray(ary1));

Output:-

true

16. fill():-

This method is used to fill the element with the given static value. it takes three argument also "value", "start index", "end index".

Example:-

let ary1=["Rahul","Shivani","Hariom","Aashish","Gopal",56,"Aashish"]
// ary1.fill("hello")
ary1.fill("Good Evening",2,5)
console.log(ary1);

Output:-

[
  'Rahul',
  'Shivani',
  'Good Evening',
  'Good Evening',
  'Good Evening',
  56,
  'Aashish'
]

17. map()

This method doesn't change the existing array. it return a new array including our calculations/operation applied on it.

let ary1=[78,2413,81,144,225]
let ary2=ary1.map((n)=> n*2)
console.log(ary2);

Output:-

[ 156, 4826, 162, 288, 450 ]

19. filter()

This method is used to filter the elements in the array based on the given conditions

let price=[2000,6000,7000,10000,3500,12000,6953,71582,9600]
let spclprce=price.filter(n => n<=10000)
console.log(spclprce);

Output:-

[
  2000,  6000,
  7000, 10000,
  3500,  6953,
  9600
]

20. for..of:-

The for...of statement executes a loop that operates on a sequence of values sourced from an iterable objects.

Example:-

let r=["aashish","shivam","sonu","monu","dixit"]
let u=[];
for(let x of r){
    u.push(x.toUpperCase())

}
console.log(u);

Output:-

[ 'AASHISH', 'SHIVAM', 'SONU', 'MONU', 'DIXIT' ]

Well! that's a quite long article hope it's useful and valuable to you..