Array and Array Methods

  • Arrays are basically single objects that contain multiple values stored in a list
  • JavaScript array is an object that represents a collection of similar type of elements.
  • There are 2 ways to construct array in JavaScript
    • By array literal
    • By using an Array constructor (using new keyword)

Create some arrays

// By array literal
const numbers = [43,56,33,23,44,36,5];
const fruit = ['Apple', 'Banana', 'Orange', 'Pear'];
const mixed = [22, 'Hello', true, undefined, null, {a:1, b:1}, new Date()];
console.log(mixed)

//By JavaScript array constructor (new keyword)
// Not recomended
const numbers2 = new Array(22,45,33,76,54);

// Empty arrays declaration
let arr1 = new Array();
let arr2 = [];

Assigning values in Array

let arr = ["Ilya", "Kantor"]
let [firstName, surname] = arr;
console.log(arr)

Access/Get an array value

// Get single value
let firstName2 = arr[0]; // Ilya
let surname2 = arr[1];  // Kantor

MUTATING ARRAYS

  • arr.push(element) –> Add an element at the end
  • arr.shift(element) –> Add an element at front
  • arr.pop() –> Remove an element from end
  • arr.unshify() –> Remove an element from front
// Insert and Remove operation
const numbers = [43,56,33];

numbers[0] = "John";
// John,56,33
numbers.push(250);
// John,56,33,250
numbers.unshift(120); 
// 120,John,56,33,250
numbers.pop();  
// 120,"John,56,33    
numbers.shift();
// "John,56,33

Array Methods

  • arr.length
  • Array.isarray(arr)
  • indexOf(num)
  • arr.reverse()
const numbers = [43,56,36,33];

// Get array length
console.log(numbers.length)
// 4

// Check if is array
console.log(Array.isArray(numbers))
// true

// Find index of value
console.log(numbers.indexOf(36));
// 2

//Reverse
numbers.reverse();
// [33, 36, 56, 43]

SORTING of STRING and NUMBERS of ARRAYS

  • sort() is better for String bad for Numbers
const numbers = [433,56,36,332];
const fruit = ['Mang', 'App', 'Apa', 'Kwi']

console.log(fruit.sort())
// ["Apa", "App", "Kwi", "Mang"]

// issue -- sorting by first no
console.log(numbers.sort())
// [332, 36, 433, 56]

// Use the "compare function" for above issue
// x - y --> Ascending
// y - x --> Decending
val = numbers.sort(function(x, y){
  return x - y;
 });
console.log(val)
// [36, 56, 332, 433]

 //using arrow fxn
numbers.sort((a, b) => a - b );
console.log(numbers)
// [36, 56, 332, 433]
  • arr.concat(arr2)
  • arr.find()
// Concatenate array
console.log(numbers.concat(numbers2))

// find()
//'num' starts from first elements in the array
// Display first no in array over 56
function over56(num){
  return num > 56;}
console.log(numbers.find(over56))
// 433

Delete element from array

  • delete arr[index] –> Not used for array
  • arr.splice(index, count) –> from index remove count elements
let arr3 = ["I", "go", "home"];
delete arr3[1]; // remove "go"
console.log( arr3); // ["I",  , "home"];
arr3.length // 3

// Splice... Actual way to del
let arr4 = ["I", "study", "JavaScript",'hii', 'bye'];
// from index 2 remove 2 element
arr4.splice(2, 2);
console.log(arr4); // ["I", "study", "bye"]

// Splice.. remove and replace
let arr5 = ["I", "study", "JavaScript", "right", "now"];
// from index 2 remove 3 element and replace them with another
arr5.splice(2, 3, "Let's", "dance");
console.log( arr5 ) // ["I", "study", "Let's", "dance"]
  • arr.slice(start, end-1)
//slice
let str = "test";
let arr6 = ["t", "e", "s", "t"];

console.log(str.slice(1, 3)); 
// es
console.log(arr6.slice(1, 3)); 
// e,s

console.log(str.slice(-2)); 
// st
console.log(arr6.slice(-2));
 // s,t

Searching in array

  • arr.indexOf(item, start) –> looks for item
    • starting from index-start, and returns the index
    • where it was found, otherwise -1.
  • arr.lastIndexOf(item, start) –> same, but looks from right to left.
  • arr.includes(item, from) –> looks for item starting from index from, returns true if found.
console.log(arr6.indexOf('t',1)); 
// 3

console.log(arr6.lastIndexOf('t')); 
// 3  

Split, Join

  • str.split(element) –> split string wrt element
  • arr.join(element) –> Join array wrt elements
let str2 = "test";
console.log(str2.split('') ); 
// t,e,s,t

let names = 'Bilbo, Gandalf, Nazgul';
console.log(names.split(', '))  
//  ["Bilbo", "Gandalf", "Nazgul"]

let arr8 = 'Bilbo, Gandalf, Nazgul, Saruman';
console.log(arr8.split(', ', 2)); 
// Bilbo, Gandalf

let arr9 = ['Bilbo', 'Gandalf', 'Nazgul'];
console.log(arr9.join('-')); // Bilbo-Gandalf-Nazgul

Reference