Open In App

JavaScript Indexed Collections

Javascript Indexed Collections are collections that have numeric indices i.e. the collections of data that are ordered by an index value. In JavaScript, an array is an indexed collection. An array is an ordered set of values that has a numeric index.

For example, an array called ‘student’ contains the name 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:

Syntax:



let arr = new Array( element0, element1, ... );   
let arr = Array( element0, element1, ... );       
let arr = [ element0, element1, ... ];  

Syntax:

let arr = new Array(6); 

let arr = Array(6);

let arr = [];
arr.length = 6;
// 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:

Method 1: Accessing the Array Elements

Use indices to access array elements. Indices of Arrays are zero-based which means the index of the elements begins with zero. 




let fruits = ['Apple', 'Mango', 'Banana'];
console.log(fruits [0]);
console.log(fruits[1]);

Output
Apple
Mango

Method 2: Obtaining array length

To obtain the length of an array use array_name.length property




let fruits = ['Apple', 'Mango', 'Banana'];
console.log(fruits.length)

Output
3

Method 3: Iterating over arrays

There are many ways to iterate the array elements.




const fruits = ['Apple', 'Mango', 'Banana'];
for (let i = 0; i < fruits.length; i++) {
    console.log(fruits[i]);
}

Output
Apple
Mango
Banana




const fruits = ['Apple', 'Mango', 'Banana'];
fruits.forEach(function (fruit) {
    console.log(fruit);
});

Output
Apple
Mango
Banana




const fruits = ['Apple', 'Mango', 'Banana'];
fruits.forEach(fruit => console.log(fruit));

Output
Apple
Mango
Banana

Method 4: Array Methods

There are various array methods available to us for working on arrays. These are:




let numbers = new Array('1', '2');
numbers.push('3');
console.log(numbers);

Output
[ '1', '2', '3' ]




let numbers = new Array('1', '2', '3');
let last = numbers.pop();
console.log(last);

Output
3




let myArray = new Array('1', '2', '3');
myArray = myArray.concat('a', 'b', 'c');
console.log(myArray);

Output
[ '1', '2', '3', 'a', 'b', 'c' ]




let students = new Array('john', 'jane', 'joe');
let list = students.join(' - ');
console.log(list);

Output
john - jane - joe




let myArray = new Array('West', 'East', 'South');
myArray.sort();
console.log(myArray);

Output
[ 'East', 'South', 'West' ]




let myArr = ['a', 'b', 'a', 'b', 'a'];
console.log(myArr.indexOf('b'));

Output
1




let myArr = new Array('a', 'b', 'c');
let first = myArr.shift();
console.log(first);

Output
a




let myArr = new Array('a', 'b', 'c');
myArr.reverse();
console.log(myArr);

Output
[ 'c', 'b', 'a' ]




let myArr1 = ['a', 'b', 'c'];
let a2 = myArr1.map(function (item) {
    return item.toUpperCase();
});
console.log(a2);

Output
[ 'A', 'B', 'C' ]




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 ]



Article Tags :