An array is a data type that allows you to store multiple values in a single variable.
Get | Add | Remove | Modify | Others |
---|---|---|---|---|
indexOf() |
push() |
pop() |
map() |
join() |
find() |
concat() |
shift() |
forEach() |
length |
filter() |
splice() |
sort() |
reduce() |
|
slice() |
reverse() |
some() |
||
every() |
IndexOf() method gets the index
of a specified element in the array.
//Example array
const array = [1,2,3,4,5];
let a = array.indexOf(3);
console.log(a);
//Output: 2
find() method get the first element
that satisfies a condition.
// Example array
const array = [1, 2, 3, 4, 5];
// Using find() to find the first even number
let c = array.find(num => num % 2 === 0);
console.log(c); // Output: 2
filter() method get an array of elements
that satisfies a condition.
const array = [1, 2, 3, 4, 5];
// Using filter() to find all even numbers
let d = array.filter(num => num % 2 === 0);
console.log(d); // Output: [2, 4]
slice() method get a subset of the array
from start index to end index end not included.
const array = ["a", "b", "c", "d","e"];
let e = array.slice(1,4)
console.log(e)
//Output: ['b','c','d']
push() will modify the original array
itself.
let array1 = [1, 2];
// Using push()
array1.push(3, 4);
console.log(array1);
// Output: [1, 2, 3, 4]
concat() method will create the neww array
and not modify the original array.
let array2 = [5, 6];
// Using concat()
let array3 = array2.concat(7, 8);
console.log(array3); // Output: [5, 6, 7, 8]
console.log(array2);
//original array is not modified
// Output: [5, 6]
pop() will remove the last element
of the array
// Using pop()
let arr1 = [1, 2, 3, 4];
let popped = arr1.pop();
console.log(popped); // Output: 4
console.log(arr1); // Output: [1, 2, 3]
shift() will remove the first element
of the array
// Using shift()
let arr2 = [1, 2, 3, 4];
let shifted = arr2.shift();
console.log(shifted); // Output: 1
console.log(arr2); // Output: [2, 3, 4]
The splice() method is used to add, remove or replace
elements in an array.
array.splice(startIndex, deleteCount, ...itemsToAdd);
let letters = ['a', 'b', 'c'];
// Add 'x' and 'y' at index 1
letters.splice(1, 0, 'x', 'y');
console.log(letters);
// Output: ['a', 'x', 'y', 'b', 'c']
// Removes 1 element starting from index 1
letters.splice(1, 1);
console.log(letters);
// Output: ['a', 'y', 'b', 'c']
// Replaces the element at index 2 with 'q'
letters.splice(2, 1, 'q');
console.log(letters);
// Output: ['a', 'y', 'q', 'c']
The slice()
method is used to get a subset of the array from the start index to the end index (end not included).
The splice()
method is used to add, remove, or replace
elements in an array.
The map()
method is used when you want to modify each element of an array and create a new array
with the modified values.
// Using map()
let arr1 = [1, 2, 3];
let mapArray = arr1.map((e) => e * 2);
console.log(mapArray); // Output: [2, 4, 6]
The forEach() method is used when you want to perform some operation on each element of an array without creating a new array
.
// Using forEach()
let arr2 = [1, 2, 3];
arr2.forEach((e) => {
console.log(e * 2);
});
// Does not return anything
// Output: 2 4 6
console.log(arr2); // Output: [1, 2, 3]
Array can be sorted or reversed by using sort()
and reverse()
methos of array.
let array = ['c', 'e', 'a', 't'];
// Sort the array
array.sort();
console.log(array);
// Output: ['a', 'c', 'e', 't']
// Reverse the array
array.reverse();
console.log(array);
// Output: ['t', 'e', 'c', 'a']
Array destructuring allows you to extract elements from an array and assign them to individual variables
in a single statement.
Array destructuring is introduced in ECMAScript 6 (ES6)
.
// Example array
const fruits = ['apple', 'banana', 'orange'];
// Array destructuring
const [firstFruit, secondFruit, thirdFruit] = fruits;
// Output
console.log(firstFruit); // Output: "apple"
console.log(secondFruit); // Output: "banana"
console.log(thirdFruit); // Output: "orange"
Array-like objects are objects that have indexed elements and a length property, similar to arrays
but they may not have all the methods of arrays like push(), pop() & others.
- arguments
- Strings
- HTML collections
sum(1, 2, 3);
// Arguments Object
function sum() {
console.log(arguments); // Output: [1, 2, 3]
console.log(arguments.length); // Output: 3
console.log(arguments[0]); // Output: 1
}
// String
const str = "Hello";
console.log(str); // Output: Hello
console.log(str.length); // Output: 5
console.log(str[0]); // Output: H
// Accessing HTML collection
var boxes = document.getElementsByClassName('box');
// Accessing elements in HTML collection using index
console.log(boxes[0]);
// Accessing length property of HTML collection
console.log(boxes.length);
- Array.from()
- Spread Syntax(...)
- Array.prototype.slice.call()
// Example array-like object
var arrayLike = {0: 'a', 1: 'b', 2: 'c', length: 3};
// Using Array.from()
var array1 = Array.from(arrayLike);
console.log(array1);
// Output: ['a', 'b', 'c']
// Using spread syntax (...)
var array2 = [...arrayLike];
console.log(array2);
// Output: ['a', 'b', 'c']
// Using Array.prototype.slice.call()
var array3 = Array.prototype.slice.call(arrayLike);
console.log(array3);
// Output: ['a', 'b', 'c']