All About Array
What is an array?
In JavaScript, an array is a data structure that allows you to store and manipulate a collection of values or objects. An array is essentially an ordered list of elements that can be accessed using an index or a key. Arrays start at index zero and can be manipulated with various methods. you can also store different data types in one array.
let myArray = [1, "two", {name: "John", age: 30}, true];
Array methods
Arrays are objects that have methods and properties built into them. , such as length
, push()
, pop()
, shift()
, unshift()
, slice()
, and more.
let arr = [0, 1, 2, 3, 4]
toString()
The JavaScript method toString()
converts an array to a string of (comma separated) array values.
let Fruits = ["Apple", "Banana", "Grapes"];
console.log(Fruits.toString());
// Apple, Banana, Grapes
Length()
The length
property returns the number of elements in the array.
// Create an array of numbers
const numbers = [1, 2, 3, 4, 5];
// Find the length of the array
const length = numbers.length;
console.log(length); // Output: 5
Push()
The push()
method adds an element to the array.
let Fruits = ["Apple", "Banana", "Grapes"];
Fruits.push("Mango");
// ["Apple", "Banana", "Grapes", "Mango"];
Pop()
The pop()
method removes an element's last position in the array.
let Fruits = ["Apple", "Banana", "Grapes", "Mango"];
Fruits.pop()
// ["Apple", "Banana", "Grapes"]
Shift()
The shift()
method removes the first element of an array.
let Fruits = ["Apple", "Banana", "Grapes", "Mango"];
Fruits.shift();
// Banana, Grapes, Mango
Unshift()
The unshift()
method adds a new element to an array.
let Fruits = ["Apple", "Banana", "Grapes"];
Fruits.unshift("Mango");
// Mango, Apple, Banana, Grapes
Concat()
The concat()
method is used to merge two or more arrays into a single array.
let Fruits = ["Apple", "Banana", "Grapes"];
let numbers = [1, 2, 3, 4, 5];
Fruits.concat(numbers)
// Apple, Banana, Grapes, 1, 2, 3, 4, 5
indexOf()
indexOf()
returns the index of the first occurrence of a specified element in an array.
let Fruits = ["Apple", "Banana", "Grapes", "Mango"];
Fruits.indexOf("Grapes")
// 2
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison', 2));
// output: 4
lastIndexOf()
lastIndexOf()
returns the index of the last occurrence of a specified element in an array.
let Fruits = ["Apple", "Banana", "Grapes", "Mango"];
Fruits.lastIndexOf()
// 3
Slice()
The slice()
method returns a new array containing a portion of the original array. It takes two arguments: the starting index (inclusive) and the ending index (exclusive) of the portion to be sliced. If either argument is negative, it counts from the end of the array.
let numbers = [1, 2, 3, 4, 5];
// Get the elements from index 2 (inclusive) to index 4 (exclusive)
let slicedArray = numbers.slice(2, 4);
console.log(slicedArray); // Output: [3, 4]
Splice()
The splice()
method changes the contents of an array by adding or removing elements. It takes three or more arguments: the starting index, the number of elements to remove (if any), and any new elements to add (if any). If the number of elements to remove is 0, no elements are removed.
const numbers = [1, 2, 3, 4, 5];
// Remove 2 elements starting from index 2, and insert 'a' and 'b' at index 2
const removedElements = numbers.splice(2, 2, 'a', 'b');
console.log(removedElements); // Output: [3, 4]
console.log(numbers); // Output: [1, 2, 'a', 'b', 5] (original array is changed)
const removedElements = numbers.splice(2 , 0, 'a', 'b');
console.log(numbers); // Output: [1, 2, 'a', 'b', 3, 4, 5] (original array is changed)
// splice(startIndexNo, deleteCount, item1, item2, itemN)
Join()
The join()
method returns a string that concatenates all the elements of an array, separated by a specified separator. If no separator is provided, a comma is used as the default separator.
const words = ['apple', 'banana', 'orange'];
// Join the elements of the array into a single string separated by a hyphen
const hyphenatedString = words.join('-');
console.log(hyphenatedString); // Output: "apple-banana-orange"
Map()
Iterates over an array and performs an action.
const num = [1, 2, 3, 4];
let arr = num.map((value) => value * value)
console.log(arr) // [ 1, 4, 9, 16 ]
Filter()
Iterates over an array and filters out an element.
const fruits = ["apple", "banana", "orange", "kiwi", "grape"];
const arr = fruits.filter((val) => val.includes("range"))
console.log(arr) // [ 'orange' ]
Reduce()
The reduce()
method takes a callback function as its first argument, which is executed for each element in the array. The callback function takes two arguments: an accumulator, which is the value returned from the previous iteration of the callback function, and the current value, which is the current element being processed in the array.
const num1 = = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const sum = num1.reduce( (ассumaltor, current) => accumaltor + current, 1 ));
console. log(sum); // 46
reduce(function(accumulator, currentValue) {
return accumulator + currentValue;
});