Javascript Array Method

There are four type of array method in JavaScript like Add/Remove Items, Searching in Array, Transform the array, Iterate over elements, Additional
Add/Remove Items
- push(…items) Adds items to the end. Example: const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.push(“Kiwi”); - pos() Extract an item from the end. Example: const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.pop(); - shift() Extract an item from the beginning. Example: const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.shift(); - unshift() Adds item from the beginning. Example: const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.unshift(); - splice(pos, deleteCount, ….items) At index pos delete deleteCount elements and insert items. Example: const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.splice(2, 0, “Lemon”, “Kiwi”); - slice(start, end) Create a new array, copies element from index start till end(not inclusive) into it. Example: const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
const citrus = fruits.slice(1, 3);
7. concat(…items) returns a new array . copies all members of the current one and adds items to it, if any of items is an array, then its elements are taken. Example: const hege = [“Cecilie”, “Lone”]; const stale = [“Emil”, “Tobias”, “Linus”]; const children = hege.concat(stale);
Searching in Array
1. indexOf() The indexOf() method searches the array for the specified item, and returns its position. Example: const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; const a = fruits.indexOf(“Apple”);
2. find() The find() method returns the value of the first element in an array that pass a tes. Example: const ages = [3, 10, 18, 20];
function checkAdult(age) {
return age >= 18;
}
function myFunction() {
document.getElementById(“demo”).innerHTML = ages.find(checkAdult);
}
3. filter()The filter() method creates an array filled with all array elements that pass a test Example: var ages = [32, 33, 16, 18];
function checkAdult(age) {
return age >= 18;
}
function myFunction() {
document.getElementById(“demo”).innerHTML = ages.filter(checkAdult);
}
Transform the Array
1. map(func) Create a new array from results of calling func for every element. Exmaple: const numbers = [4, 9, 16, 25]; const x = numbers.map(Math.sqrt) document.getElementById(“demo”).innerHTML = x;
2. reduce(func, intial) Calculate a single value over the array by calling func for each element and passing an intermediate result between the calls. Example: const numbers = [175, 50, 25]; document.getElementById(“demo”).innerHTML = numbers.reduce(myFunc);
function myFunc(total, num) {
return total — num;
}
3. reverse() Reverse the array in-place, then return it. Example: const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; fruits.reverse();
Iterate over element
1. forEach(func) calls function for every element, dose not return anything. Example: const fruits = [“apple”, “orange”, “cherry”]; fruits.forEach(myFunction);
function myFunction(item, index) {
document.getElementById(“demo”).innerHTML += index + “:” + item + “<br>”;
}