Array
An array is a same type(homogenous) collection of values.
It is a user-defined type.
Arrays are static. This means that an array once initialized cannot be resized.
Array element values can be updated or modified but cannot be deleted.
Some new functions of array are:- Array.of(), Array.from(), Array.fill(), Array.find(), Array.findIndex(), Array.copyWithin(), Array.entries(), Array.keys(), and Array.values().
Syntax-
let array_name = new Array(); >// By using the new keyword
let array_name = [value1, value2,....valueN]; >//By using Array literals
array_name=[value1, value2,…..valueN]; >//Initialization
Example:-
let Data;
Data = ["Aman","raman","3","sanam"]
console.log(Data[0]);
console.log(Data[1]);
console.log(Data[3]);
Output:-
Aman
raman
sanam
Array.of()
Array.of() is a new way of creating an array.
If you create an array with just one numeric value, the array is created with just that value and not the amount of that value.
Example 1:-
let Data = Array.of('aman' , 'kamal');
console.log(Data.length);
Output:-
2
Example 2:-
let amount = Array.of(500);
let amount2= Array.of(500,100);
console.log(amount.length);
console.log(amount2.length);
Output:-
1
2
Array.from()
Example 1:-
console.log(Array.from('Aman'));
Output:-
["A", "m", "a", "n"]
Example 2:-
let Amount = [300, 800, 1000];
let percent = Array.from(Amount, calculate => calculate * 1.02);
console.log(percent);
Output:-
[306, 816, 1020]
Array.fill()
This function will overwrite any existing values in all keys of the array with the provided value.
Example 1:-
let values = [500, 700, 1000];
values.fill(7000);
console.log(values);
Output:-
[7000, 7000, 7000]
Example 2: where we override value at position by passing second arrgument:-
let values = [500, 700, 1000, 9000];
values.fill(7000,3);
console.log(values);
Output:-
[500, 700, 1000, 7000]//here at 3rd position its override
Array.find()
This function will overwrite any existing values in all keys of the array with the provided value.
Example 1:-
let Data = [10,20,30,40,50,60];
let result = Data.find(amount => amount > 45);
console.log(result);
Output:-
50
Array.findIndex()
It returns the index instead of returning the value.
It is almost same as Array.find().
Example 1:-
let data = [5, 12, 8, 130, 44];
let number = (element) => element > 15;
console.log(data.findIndex(number));
Output:-
3//It shows that at position 3 the number is greater than 15.
Array.copyWithin()
It copies the values inside the array one place to another place.
It returns without modifying its length.
Example 1:-
let data = [100, 200, 300, 400, 500, 600, 700];
// copy to index 1 all elements from index 3 to the end
data.copyWithin(1, 3);
console.log(data);
Output:-
[100, 400, 500, 600, 700, 600, 700]
Example 2:-
let data = [100, 200, 300, 400, 500, 600, 700];
// copy to index 0 the element at index 3
data.copyWithin(0, 3, 4);
console.log(data);
Output:-
[400, 200, 300, 400, 500, 600, 700]
Array.entries()
This method returns a new Array Iterator object that contains the key/value pairs for each index in the array.
Example 1:-
let data = ['Aman', 'Raman', 'Kamal'];
console.log(data.entries());
Output:-
Array Iterator {}
Example 2 now use next() method:-
let data = ['Aman', 'Raman', 'Kamal'];
let data2 = data.entries();
console.log(data2.next().value);
console.log(data2.next().value);
Output:-
[0, "Aman"]
[1, "Raman"]
Array.keys()
It is similar to Array.entries() except that it only provides the keys of the array.
Example 1:-
let data = ['Aman', 'Raaman', 'Kamal'];
console.log(...data.keys());
Output:-
0 1 2
Example 2 with for loop:-
let data = ['Aman', 'Raman', 'Kamal'];
let data2 = data.keys();
for (let key of data2)
{
console.log(key);
}
Output:-
0
1
2
Array.values()
It returns a new Array Iterator object that contains the values for each index in the array.
Example 1:-
let data = ['Aman', 'Raaman', 'Kamal'];
console.log(...data.values());
Output:-
Aman Raaman Kamal
Example 2 with for loop:-
let data = ['Aman', 'Raman', 'Kamal'];
let data2 = data.values();
for (let value of data2)
{
console.log(value);
}
Output:-
Aman
Raman
Kamal
Array.join()
It 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.
Example 1:-
let data = ['Aman', 'Raaman', 'Kamal'];
console.log(data.join());
console.log(data.join(''));
console.log(data.join('-'));
Output:-
Aman,Raaman,Kamal
AmanRaamanKamal
Aman-Raaman-Kamal
Array.join()
It returns the first index at which a given element can be found in the array, or -1 if it is not present.
Example 1:-
let data = ['Aman', 'Raaman', 'Kamal'];
console.log(data.indexOf('Raaman'));
console.log(data.indexOf('Raaman', 2));
Output:-
1
-1