Array Methods
There are various array methods available to us for working on arrays. These are:
- JavaScript push() Method: This method adds one or more elements to the end of an array and returns the resulting length of the array.
let numbers = new Array('1', '2');
numbers.push('3');
console.log(numbers);
Output
[ '1', '2', '3' ]
- JavaScript pop() Method: This method removes the last element from an array and returns that element.
let numbers = new Array('1', '2', '3');
let last = numbers.pop();
console.log(last);
Output
3
- JavaScript concat() Method: This method joins two arrays and returns a new array.
let myArray = new Array('1', '2', '3');
myArray = myArray.concat('a', 'b', 'c');
console.log(myArray);
Output
[ '1', '2', '3', 'a', 'b', 'c' ]
- JavaScript join() Method: This method creates a string by joining all the elements of an array.
let students = new Array('john', 'jane', 'joe');
let list = students.join(' - ');
console.log(list);
Output
john - jane - joe
- JavaScript sort() Method: This method sorts the elements of an array.
let myArray = new Array('West', 'East', 'South');
myArray.sort();
console.log(myArray);
Output
[ 'East', 'South', 'West' ]
- JavaScript indexOf() Method: This method searches the array for an Element and returns the index of the first occurrence of the element.
let myArr = ['a', 'b', 'a', 'b', 'a'];
console.log(myArr.indexOf('b'));
Output
1
- JavaScript shift() Method: This method removes the first element from an array and returns that element.
let myArr = new Array('a', 'b', 'c');
let first = myArr.shift();
console.log(first);
Output
a
- JavaScript reverse() Method: This method reverses the first array element become the last and the last becomes the first. It transposes all the elements in an array in this manner and returns a reference to the array.
let myArr = new Array('a', 'b', 'c');
myArr.reverse();
console.log(myArr);
Output
[ 'c', 'b', 'a' ]
- JavaScript map() Method: This method returns a new array of the returned value from executing a function on every array item.
let myArr1 = ['a', 'b', 'c'];
let a2 = myArr1.map(function (item) {
return item.toUpperCase();
});
console.log(a2);
Output
[ 'A', 'B', 'C' ]
- JavaScript filter() Method: This method returns a new array containing the items for which the function returned true.
let myArr1 = ['a', 10, 'b', 20, 'c', 30];
let a2 = myArr1.filter(function (item) {
return typeof item === 'number';
});
console.log(a2);
Output
[ 10, 20, 30 ]
JavaScript Indexed Collections
Indexed collections in JavaScript refer to data structures like arrays, where elements are stored and accessed by numerical indices. Arrays allow for efficient storage and retrieval of ordered data, providing methods for manipulation and traversal of their elements.
Example
an array called ‘student’ contains the names of the students and the index values are the Roll Numbers of the students. JavaScript does not have an explicit array data type. However, we can use the predefined Array object in JavaScript and its methods to work with arrays.
Creating an Array: There are many ways to create and initialize an array that is listed below:
- Creating arrays without defining the array length. In this case, the length is equal to the number of arguments.
Syntax:
let arr = new Array( element0, element1, ... );
let arr = Array( element0, element1, ... );
let arr = [ element0, element1, ... ];
- Creating an array with the given size
Syntax:
let arr = new Array(6);
let arr = Array(6);
let arr = [];
arr.length = 6;
- Create a variable-length array and add many elements as you need.
// First method: Initialize an empty
// array then add elements
let students = [];
students [0] = 'Sujata Singh';
students [1] = 'Mahesh Kumar';
students [2] = 'Leela Nair';
// Second method: Add elements to
// an array when you create it
let fruits = ['apple', ‘mango', 'Banana'];
The methods that can be applied over arrays are:
- Accessing the Array Elements
- Obtaining array length
- Iterating over arrays
- Array Methods