Array & Methods of Array in JavaScript.
This Blog let us understand Array and what are the different methods of Arrays in JavaScript.
Array :
The Array object uses to store a collection of multiple items under a single variable name. JavaScript arrays are resizable and can contain a mix of different DataType. it is Zero-indexed.
Creating an Array:
- There are two ways to declare an array:
let names = ['Peter','Harry','tony',2,6,true];
(Generally not used in code practices)
let names = new Array('Peter','Harry','tony',2,6,true);
Printing an Array:
console.log(names);
//printing spacific output from index
console.log(names[2]);
Methods of Array in JavaScript:
Length()
Use it to get the number of elements in that array.
let names = new Array('Peter','Harry','tony',2,6,true);
console.log(names.length());
output:-6
Push()
The
push()
the method adds one or more elements to the end of an array and returns the new length of the array.
let names = ['Yugal', 'Anurag', 'Surya', 'Anirudh', 'Bipul'];
names.push('VYOM');
console.log(names);
output: [ 'Yugal', 'Anurag', 'Surya', 'Anirudh', 'Bipul', 'VYOM' ]
Slice()- for printing multiple value
The
slice()
method returns a shallow copy of a portion of an array into a new array object selected fromstart
toend
(end
not included) wherestart
andend
represent the index of items in that array. The original array will not be modified.let names = ['Yugal', 'Anurag', 'Surya', 'Anirudh', 'Bipul']; console.log(names.slice(1, 4));
output: [ 'Anurag', 'Surya', 'Anirudh' ]
- splice()
The splice()
method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
let fruit = ['Apple', 'Bada Apple', 'Chota Apple', 'Double Apple'];
fruit.splice(2, 1, 'Kharab Apple', 'Acha Apple');
console.log(fruit);
In this method, 2 is the index where data puts. 1 is no of elements to be removed it can be 0. and the rest is the data that we want to put.
output:[ 'Apple', 'Bada Apple', 'Kharab Apple', 'Acha Apple', 'Double Apple' ]
Concatenation
This method is use to add tow or more arrays.
let arr1 = [1, 2, 3, 4];
let arr2 = [4, 5, 6, 7];
let arr3 = [6, 7, 6, 7,7,7,7,7,7,7,7,7];
console.log(arr1.concat(arr2));
console.log(arr1.concat(arr2, arr3));
output:
[ 1, 2, 3, 4, 4, 5, 6, 7 ]
[ 1, 2, 3, 4, 4, 5, 6, 7, 6, 7, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7 ]
Fill
It adds the date to a certain index to end index(exclude) that we need to give , it replaces the date and return the modified array.
let arr4 = [1, 2, 3, 4, 5, 6, 7, 8, 9]; arr4.fill('Anurag',2, 4); console.log(arr4);
output: [1,2,'Anurag','Anurag',4,5,6,7,8,9]
Includes
gives output as True or False, The
includes()
method determines whether an array includes a certain value among its entries.let arr = ['yugal','ram','pj','krish']; console.log(arr.includes('pj',2);
output: True
IndexOf
The
indexOf()
method returns the first index at which a given element can be found in the array, or -1 if it is not present.let num = [1, 2, 3, 'Anurag', 4, 5, 6, 7, 8, 'Anurag', 'Anurag']; console.log(num.indexOf('Anurag'));
Output: 3
isArray
it gives true or False output, it confirms whether a given input is an array or not.
let num = [1, 2, 3, 'Anurag', 4, 5, 6, 7, 8, 'Anurag', 'Anurag']; let num1 = 'Anurag'; console.log(Array.isArray(num1)); console.log(Array.isArray(num));
> `Output:`
>
> `False`
>
> `True`
join()
The
join()
method creates and returns a new string by concatenating all of the elements in an array, separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.let Arr1 = [1, 2, 3, 4, 5, 6, 7]; let var1 = Arr1.join(' and '); console.log(var1);
Output:1 and 2 and 3 and 4 and 5 and 6 and 7
lastIndexOf
The
lastIndexOf()
method returns the last index at which a given element can be found in the arraylet num = [1, 2, 3, 'Anurag', 4, 5, 6, 7, 8, 'Anurag', 'Anurag']; console.log(num.indexOf('Anurag'));
Output: 3
(imp) Map()
this method is applied to each element in the array and it does not change the original array.the results of calling a provided function on every element in the calling array.
let num = [1,2,3,4,5]; console.log(num.map(Math.sqrt));
Output: [1,4,9,16,25]
Pop()
The
pop()
method removes the last element from an array and returns that element. This method changes the length of the array.let maths = [1, 4, 9, 16, 25]; console.log(maths.pop()); console.log(maths)
Output: 25
[1,4,9,16]
Reverse
elements order in the array will be turned towards the direction opposite to that previously stated.
let num = ['one','two','three','four']; console.log(num.reverse());
Output: ['Four ','three','two',one']
shift
The
shift()
method removes the first element from an array and returns that removed element. This method changes the length of the array.let num = [1,2,3,4,5] console.log(num.shift()); console.log(num);
Output: 1
[2,3,4,5]
sort
It sorts the element and changes the order of elements into ascending order.
let num = ['Surya', 'Anirudh', 'Bipul']; console.log(num.sort()); const array1 = [1, 30, 4, 21, 100000]; array1.sort(); console.log(array1);
Output: [Anirudh','Bipul','Surya']
[1, 100000, 21, 30, 4]
unshift
The
unshift()
method adds one or more elements to the beginning of an array and returns the new length of the array.let fruit = ['Apple', 'Bada Apple', 'Chota Apple', 'Double Apple']; fruit.unshift('Apple 1', 'Grapes 2'); console.log(fruit);
['Apple 1', 'Grapes 2', 'Apple', 'Bada Apple', 'Chota Apple', 'Double Apple' ]
Converting to Array
convert any element(String) into array
let name= 'Yugal'; let arr = name.split(''); console.log(arr);
Output: ['Y', 'u',' g',' a','l']
(imp) for of
The
forEach()
method executes a provided function once for each array element.let fruit = ['Apple', 'Bada Apple', 'Chota Apple', 'Double Apple']; let Upperfruit = []; // empty Array for (let name of fruit) { Upperfruit.push(name.toUpperCase()); } console.log(Upperfruit);
Output:
[ 'APPLE', 'BADA APPLE', 'CHOTA APPLE', 'DOUBLE APPLE' ]
Key
The
keys()
method returns a new Array Iterator object that contains the keys for each index in the array.const array1 = ['a', 'b', 'c']; const iterator = array1.keys(); for (const key of iterator) { console.log(key); }
Output: 0
1
2
Hope get the idea about the Array of the JavaScript .
thank You !