Array
Arrays are resizable and contains elements of different data types, It can be accessible using index values arrays always starts with 0.
Declaring array
const number = [1, 2, 3, 4, 5]; // or
const number = new Array(1, 2, 3, 4, 5);
Important array functions
at()
Returns the element at position mentioned.
const array1 = [5, 12, 8, 130, 44];
console.log(array1.at(2));
// Output : 8
concat()
Used to merge two or more arrays.
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = [1, 2, 3];
console.log(array1.concat(array2, array3));
// Output : ['a', 'b', 'c', 'd', 'e', 'f', 1, 2, 3]
every()
Returns true if every element in array satisfies the testing function.
function isEven(x){
return (x%2 === 0);
}
const array1 = [2, 4, 6];
console.log(array1.every(isEven));
// Output : true
fill()
Fills the static value within the positions given.
const array1 = [1, 2, 3, 4, 5, 6];
console.log(array1.fill(0, 2,4));
// Output : [1, 2, 0, 0, 5, 6]
filter()
Creates a new array based on given criteria.
const array1 = [1, 2, 3, 4, 5, 6];
console.log(array1.filter(x => x%2 === 0));
// Output : [2, 4, 6]
find()
Return the first element in array which satisfies the condition.
const array1 = [10, 2, 13, 4, 5, 13];
console.log(array1.find(x => x > 11));
// Output : 13
findIndex()
Returns the index of first element in array which satisfies the condition.
const array1 = [10, 2, 13, 4, 5, 13];
console.log(array1.findIndex(x => x > 11));
// Output : 2
findLast()
Return the last element in array which satisfies the condition.
const array1 = [10, 2, 13, 4, 5, 13, 2];
console.log(array1.findLast(x => x > 11));
// Output : 13
findLastIndex()
Returns the index of last element in array which satisfies the condition.
const array1 = [10, 2, 13, 4, 5, 13, 2];
console.log(array1.findLastIndex(x => x > 11));
// Output : 2
forEach()
Executes a provided function for each rray element.
const array1 = [1, 2, 3, 4];
array1.forEach(x => console.log(x));
// Output :
// 1
// 2
// 3
// 4
includes()
Returns if the array contain certain element or not.
const array1 = [1, 2, 3, 4];
console.log(array1.includes(2));
// Output : true
indexOf()
Returns the index of the given element.
const array1 = [1, 2, 3, 4];
console.log(array1.indexOf(2));
// Output : 1
lastIndexOf()
Returns the last index of the given element.
const array1 = [1, 2, 3, 2];
console.log(array1.lastIndexOf(2));
// Output : 3
join()
Concatenates all array element to a string.
const array1 = ['A', 'B', 'C'];
console.log(array1.join(''));
// Output : "ABC"
keys()
Returns a new array iterator object with index for each index in array.
const array1 = ['a', 'b', 'c'];
const iterator = array1.keys();
for (const key of iterator) {
console.log(key);
}
// output :
// 0
// 1
// 2
map()
Creates a new array populated with the results of calling a provided function on every element in the calling array.
const array1 = [1, 4, 9, 16];
const map1 = array1.map(x => x * 2);
console.log(map1);
// output : [2, 8, 18, 32]
pop()
Removes last element from the array.
const array1 = ['a', 'b', 'c'];
array1.pop();
console.log(array1);
// output : ['a', 'b']
push()
Adds one or more element at the end of the array.
const array1 = ['a', 'b', 'c'];
array1.push('d', 'e');
console.log(array1);
// output : ['a', 'b', 'c', 'd', 'e']
reduce()
Executes a user-supplied "reducer" callback function on each element of the array (from left to right), to reduce it to a single value.
const array1 = [1, 2, 3, 4];
const initialValue = 0;
const sumWithInitial = array1.reduce(
(previousValue, currentValue) => previousValue + currentValue,
initialValue
);
console.log(sumWithInitial);
// output: 10
Executes a user-supplied "reducer" callback function on each element of the array (from right to left), to reduce it to a single value.
const array1 = [[0, 1], [2, 3], [4, 5]];
const result = array1.reduceRight((accumulator, currentValue) => accumulator.concat(currentValue));
console.log(result);
// output : [4, 5, 2, 3, 0, 1]
reverse()
Reverses the order of the elements of an array in place.
const array1 = ['one', 'two', 'three'];
console.log(array1.reverse());
// output : ["three", "two", "one"]
shift()
Removes the first element from an array and returns that element.
const array1 = [1, 2, 3];
const firstElement = array1.shift();
console.log(array1);
// output : [2, 3]
unshift
Adds one or more elements to the front of an array, and returns the new length of the array.
const array1 = [1, 2, 3];
array1.unshift(4, 5);
console.log(array1);
// output : [4, 5, 1, 2, 3]
slice()
Extracts a section of the calling array and returns a new array.
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// output : ["camel", "duck", "elephant"]
splice()
Adds and/or removes elements from an array.
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
console.log(months);
// output : ['Jan', 'Feb', 'March', 'April', 'June']
some()
Returns true if at least one element in the calling array satisfies the provided function.
const array = [1, 2, 3, 4, 5];
const even = (element) => element % 2 === 0;
console.log(array.some(even));
// output: true
sort()
Sorts the elements of an array in place and returns the array.
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// output: ["Dec", "Feb", "Jan", "March"]
toString()
Returns a string representing the specified array and its elements.
const array1 = [1, 2, 'a', '1a'];
console.log(array1.toString());
// output: "1,2,a,1a"
toLocalString()
Returns a localized string representing the calling array and its elements.
const array1 = [1, 'a', new Date('21 Dec 1997 14:12:00 UTC')];
const localeString = array1.toLocaleString('en', { timeZone: 'UTC' });
console.log(localeString);
// output: "1,a,12/21/1997, 2:12:00 PM"
values()
Returns a new array iterator object that contains the values for each index in the array.
const array1 = ['a', 'b', 'c'];
const iterator = array1.values();
for (const value of iterator) {
console.log(value);
}
// output:
"a"
"b"
"c"
Happy Learning :)