JavaScript - arrays
IN JAVASCRIPT, ARRAYS ARE UNTYPED
Introduction
An array is an ordered collection of values
Each value is called an element
Each element has a position in array called index
An array can have elements of any types
Array elements can be objects or even other arrays
Array inherit useful methods from array.Prototype
array.prototype
The Array.prototype is an object that allows you to define methods that all JavaScript arrays can inherit. It's
essentially the prototype for all instances of Array, and you can extend it by adding custom methods.
// Adding a custom method to Array.prototype
Array.prototype.first = function() {
return this[0];
};
// Now, all arrays have access to this method
const arr = [1, 2, 3];
console.log(arr.first()); // Output: 1
//array definition
let a = new Array();
let a = new Array(10);
let a = new Array(5,4,3,2,1,”testing”, “testing”);
a=[1,2,3,4,5];
let a=[];
Multidimensional arrays
let table = new Array(10);
for(let col=0; col<table.length;col++){
table[col]=new Array(10);
}
for(let row=0; row<table.length;row++){
for(let col=0; col<table.length;col++){
table[row][col]=row*col;
}
}
for(let row=0; row<table.length;row++){
for(let col=0; col<table.length;col++){
console.log(table[row][col]);
}
}
Array methods
Mutating Methods
push() : The push() method adds a new element to an array (at the end)
pop() : The pop() method removes the last element from an array
shift() : The shift() method removes the first array element and "shifts" all other elements to a
lower index
unshift() : The unshift() method adds a new element to an array (at the beginning), and "unshifts"
older elements
splice() : The splice() method can be used to add new items to an array
sort()
reverse()
fill()
copyWithin() : The copyWithin() method copies array elements to another position in an array
let arr = [1, 2]; let arr = [2, 3]; let arr = [1, 2, 3];
arr.push(3); arr.unshift(1); arr.reverse();
console.log(arr); // [1, 2, 3] console.log(arr); // [1, 2, 3] console.log(arr); // [3, 2, 1]
let arr = [1, 2, 3]; let arr = [1, 2, 3]; let arr = [1, 2, 3, 4];
arr.pop(); arr.splice(1, 1, "a", "b"); arr.fill(0, 1, 3);
console.log(arr); // [1, 2] console.log(arr); // [1, "a", "b", 3]
console.log(arr); // [1, 0, 0, 4]
let arr = [3, 1, 2];
let arr = [1, 2, 3]; let arr = [1, 2, 3, 4];
arr.sort();
arr.shift(); arr.copyWithin(1, 2);
console.log(arr); // [1, 2, 3] (for strings
console.log(arr); // [2, 3] or with compare function) console.log(arr); // [1, 3, 4, 4]
Accessor Methods
concat()
join()
slice()
indexOf()
lastIndexOf()
includes()
find()
findIndex()
flat()
flatMap()
let arr = [1, 2];
let result = arr.concat([3, 4]);
console.log(result); // [1, 2, 3, 4]
let arr = ['a', 'b', 'c'];
console.log(arr.join('-')); // "a-b-c“
let arr = [1, 2, 3, 4];
let result = arr.slice(1, 3);
console.log(result); // [2, 3]
let arr = [1, 2, 3];
console.log(arr.indexOf(2)); // 1
let arr = [1, 2, 3, 2];
console.log(arr.lastIndexOf(2)); // 3
let arr = [1, 2, 3];
console.log(arr.includes(2)); // true
let arr = [1, 2, 3];
let result = arr.find(x => x > 1);
console.log(result); // 2
let arr = [1, 2, 3];
let index = arr.findIndex(x => x > 1);
console.log(index); // 1
let arr = [1, [2, [3]]];
console.log(arr.flat(2)); // [1, 2, 3]
let arr = [1, 2, 3];
let result = arr.flatMap(x => [x, x * 2]);
console.log(result); // [1, 2, 2, 4, 3, 6]
Iteration Methods
forEach()
map()
filter()
reduce()
reduceRight()
some()
every()
sort()
toString()
let arr = [1, 2, 3];
arr.forEach(x => console.log(x));
// 1
// 2
// 3
let arr = [1, 2, 3];
let result = arr.map(x => x * 2);
console.log(result); // [2, 4, 6]
let arr = [1, 2, 3, 4];
let result = arr.filter(x => x % 2 === 0);
console.log(result); // [2, 4]
let arr = [1, 2, 3, 4];
let sum = arr.reduce((acc, val) => acc + val, 0);
console.log(sum); // 10
let arr = ['a', 'b', 'c'];
let result = arr.reduceRight((acc, val) => acc + val);
console.log(result); // "cba“
let arr = [1, 2, 3];
console.log(arr.some(x => x > 2)); // true
let arr = [1, 2, 3];
console.log(arr.toString()); // "1,2,3"
let arr = [1, 2, 3];
console.log(arr.every(x => x > 0)); // true
x => x > 0
is an arrow function in JavaScript.
Here's what it means:
x is the parameter (input).
x > 0 is the condition being evaluated.
The function returns true if x is greater than 0, and false otherwise.
Other Methods
Array.isArray()
from()
keys()
values()
entries()
fill()
findLast()
at()
console.log(Array.isArray([1, 2, 3])); // true
let str = "abc";
let arr = Array.from(str);
console.log(arr); // ["a", "b", "c"]
let arr = Array.of(1, 2, 3);
console.log(arr); // [1, 2, 3]
let arr = ['a', 'b', 'c'];
for (let key of arr.keys()) {
console.log(key); // 0, 1, 2
}
let arr = ['a', 'b', 'c'];
for (let val of arr.values()) {
console.log(val); // "a", "b", "c"
}
let arr = ['a', 'b'];
for (let [i, val] of arr.entries()) {
console.log(i, val); // 0 "a", 1 "b"
}
let arr = [10, 20, 30];
console.log(arr.at(-1)); // 30
let arr = [1, 2, 3, 4];
let lastEven = arr.findLast(x => x % 2 === 0);
console.log(lastEven); // 4
Misc.
Array Property
length
let fruits = ['apple', 'banana', 'cherry'];
console.log(fruits.length); //3
ASSOCIATIVE ARRAYS
Associative Arrays: Key-Value pairs?
i.e., var pincodes = [];
pincodes["khanpur"] = 110062;
pincodes["badarpur"] = 110044;
pincodes["saket"] = 110017;
Accessing Associative arrays:
i.e., pincodes["saket"]; // 110017
pincodes["Badarpur"]; // You guess here.
Associative arrays are actually Objects!