Table of contents
- What is Array?
- Here we discuss some types of Array methods :
- 1.0 Array.Length :
- 2.0 Name Replace :
- 3.0 New Array:
- 4.0 Push:
- 5.0 Slice :
- 6.0 Splice:
- 7.0 Concatenation:
- 8.0 Fill :
- 9.0 Includes :
- 10.0 IndexOf:
- 11.0 IsArray:
- 12.0 Join:
- 13.0 Last IndexOf:
- 14.0 Map (for math):
- 15.0 Pop & Shift:
- 16.0 Reverse :
- 17.0 Sort:
- 18.0 unshift:
- 19.0 Split():
- 20.0 For Of:
- 8.0 Break and continue:
What is Array?
- **Array** simply means when some of the values (like - numbers, pictures, videos, some elements etc.) arrange in a row or column this is called an array.
The array can store multiple values in single variables. the array denoted by this square bracket [ ] and inside we also can use multiple values ['String', 'boolean', ' number values'] and the array we use to coma (,) for separate one to another object.
Array arranges some of the values and when we need to execute or print only one value or some of the values for requirements inside the array just give the index number of the array.
![Array](scaler.com/topics/images/1-%281%29.webp)
Examples of an array :
const flowers = ["rose", "lily","tulip"]; console.log(flowers); console.log(flowers[1])
Output :
rose, lily, tulip lily
Here we discuss some types of Array methods :
1.0 Array.Length :
Length gives you the total index length of the array.
Examples of this array :
let allNames = ['Pradip','souvik','sudip','rajdip','soumodip'];
// full length
console.log(allNames.length);
// substaction from length
console.log(allNames[allNames.length-1]);
Output :
5
soumodip
2.0 Name Replace :
This types of array make a change or replaces the position of value which position of value needs to replace or change.
Examples of this array :
let allNames = ['Pradip','souvik','sudip','rajdip','soumodip'];
allNames[3] = "RK"
console.log(allNames);
Output :
[ 'Pradip', 'souvik', 'sudip', 'RK', 'soumodip' ]
3.0 New Array:
This is another type of declare array. This means this is 2nd option to declare an array.
Examples of this array :
let sntx = new Array ('thiago','mateo');
console.log(sntx[1])
Output :
mateo
4.0 Push:
Insert a new value inside the array.
When you want to push or add extra value inside the array then use it.
Example :
let sntx = new Array ('thiago','mateo');
sntx.push('Father Messi')
console.log(sntx)
Output :
[ 'thiago', 'mateo', 'Father Messi' ]
5.0 Slice :
Slice simply means 'piece'. This means from which portion to which portion you want to print. Lets you have 10 numbers of the index to an array [like 1,2,3,4,5,6,7,8,9,10] and you want to show or print from 3 to 7 this is called by Slice.
* console.log(piece.slice(2,4));
Why do we use 2,4
2 => for use starting position for printing
4 => for use ending position for printing.
Examples of this array :
let piece = ['Pradip','souvik','sudip','rajdip','soumodip'];
console.log(piece.slice(2,4));
Output :
[ 'sudip', 'rajdip' ]
6.0 Splice:
Insert a new value inside the array as like 'push' but here have two things 'insertion and deletion'.
valu.splice(1,0,'coconut','banana');
Where
1 => index starting point for inserting.
0 => deletion means how many values you want to delete.
Example:
let valu = ['apple','bada apple','chota apple','double apple'];
valu.splice(1,0,'coconut','banana');
console.log(valu);
Output :
[
'apple',
'coconut',
'banana',
'bada apple',
'chota apple',
'double apple'
]
7.0 Concatenation:
Connecting two or other variables in the same array.
Example :
let array1 = [1,2,3,4,5];
let array2 = [6,7,8,9,10];
let array3 = [11,12,13,14,15,16,17];
console.log(array1.concat(array2,array3))
Output :
[
1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14,
15, 16, 17
]
8.0 Fill :
Fill up the same value into different indexes.
this value(messi) will be displayed between 4 to 8 no of the index.
Example :
let sntx = [1, 2, 3, 4, 5, 6, 7, 8, 9];
sntx.fill("messi", 4, 8 )
console.log(sntx);
Output :
[
1, 2,
3, 4,
'messi', 'messi',
'messi', 'messi',
9
]
9.0 Includes :
If the value and index position will be the same then it is true. And when the value and index position will be not the same then it is false.
Example :
let sntx = [1,2,3,4,"messi",5,6,7,8,9];
console.log(sntx.includes("messi", 4))
Output :
true
10.0 IndexOf:
IndexOf gives you the first index position.
Example :
let sntx = [1,2,3,4,"messi",5,6,7,8,9];
console.log(sntx.indexOf('messi'));
Output :
4
11.0 IsArray:
This is an array or not?
If this is an array then is it true otherwise false.
Example :
//array
let allNames = ['Pradip','souvik','sudip','rajdip','soumodip'];
//this is not an array
let sntx = "messi"
console.log(Array.isArray(sntx));
Output :
true
false
12.0 Join:
Join each and everyone with your declared value.
Example :
let sntx = [1, 2, 3, 4, 5, 6]
console.log(sntx.join('/'))
Output :
$ node arrayInDepth04.js
1/2/3/4/5/6
13.0 Last IndexOf:
The lastIndexOf gives you the last index position.
Example :
let sntx = [1,2,3,4,'messi',5,6,'messi',8,9];
//indexOf
console.log(sntx.indexOf('messi'))
//lastIndexOf
console.log(sntx.lastIndexOf('messi'))
Output :
//indexOf
4
//lastIndexOf
7
14.0 Map (for math):
Return a new array with the square root of all elements.
Example :
let sntx = [1,4,9,16,25,36,49,64,81,100];
let up = sntx.map(Math.sqrt);
console.log(up);
Output :
[
1, 2, 3, 4, 5,
6, 7, 8, 9, 10
]
15.0 Pop & Shift:
Pop => remove all things from the front side.
Shift => remove all things from behind.
Example :
let sntx = [1, 2, 3, 4, 5, 6]
//Pop
console.log(sntx.pop());
//Shift
console.log(sntx.shift());
Output :
6
1
16.0 Reverse :
Do reverse the array.
Example :
let sntx = [1,4,9,16,25,36,49,64,81,100]
console.log(sntx.reverse());
Output :
[
100, 81, 64, 49, 36,
25, 16, 9, 4, 1
]
17.0 Sort:
Inside an array, all string values will sort name-wise.
Example :
let sntx = ['pradip', 'souvik', 'rajdip', 'soumo', 'arun', 'krish', 'biltu'];
//sort and reverse
console.log (sntx.reverse(sntx.sort()));
//sort
console.log(sntx.short());
Output :
//sort with reverse
[
'souvik', 'soumo',
'rajdip', 'pradip',
'krish', 'biltu',
'arun'
]
//sort
[
'arun', 'biltu',
'krish', 'pradip',
'rajdip', 'soumo',
'souvik'
]
18.0 unshift:
Adding somethings from front.
Example :
let sntx = ['pradip', 'souvik', 'rajdip', 'soumo', 'arun', 'krish', 'biltu'];
sntx.unshift(1,2);
console.log(sntx);
Output :
[
1, 2,
'pradip', 'souvik',
'rajdip', 'soumo',
'arun', 'krish',
'biltu'
]
19.0 Split():
Devide a word and convert into deffirent index.
Example :
let names = 'messi';
let names1 = names ;
console.log(names1.split('')) ;
Output :
[ 'm', 'e', 's', 's', 'i' ]
20.0 For Of:
For of to convert Lowercase letter into uppercase letter.
Example :
let lowerNames = ['pradip', 'souvik', 'rajdip', 'soumo', 'arun', 'krish', 'biltu'];
let upperName = [];
for(let cV of lowerName){
upperName.push(cV.toUpperCase())
}
console.log(upperName.sort())
Output :
[
'ARUN', 'BILTU',
'KRISH', 'PRADIP',
'RAJDIP', 'SOUMO',
'SOUVIK'
]
8.0 Break and continue:
Break => Stop the printing of index value after it is used. when you use it it would be stop this point.
ex: you have 6 values [1,2,3,4,5,6] when you want to stop the array index after 4 it will not be printing 5,6.
Continue => Just skip this number and next value will be printing continuesly.
Example :
//Break
for(let i = 0; i<=50 ; i++){
if(i == 4){
break;
}
console.log(i)
}
//Continue
for(let i = 0; i<=50 ; i++){
if(i == 4){
break;
}
console.log(i)
}
Output :
//Break
0
1
2
3
//Continue
0
1
2
3
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50