Array
Create an array
var cars = ["Buick", "Acura", "Lincoln", "Honda"];
console.log(cars);
console.log(typeof(cars)); //object
var cars_2 = new Array("Buick", "Acura", "Lincoln", "Honda");
console.log(cars_2);
console.log(typeof(cars_2)); //object
Array methods
var cars = ["Buick", "Acura", "Lincoln", "Honda"];
//access element
console.log(cars[0]);
//modify element
cars[0] = "Toyota";
console.log(cars);
//length
console.log(cars.length); //4
//add element as the last element
cars.push("Subaru"); //["Toyota", "Acura", "Lincoln", "Honda", "Subaru"]
console.log(cars);
cars[cars.length] = "Benz";
console.log(cars); //["Toyota", "Acura", "Lincoln", "Honda", "Subaru", "Benz"]
//remove the last element
console.log(cars.pop()); //Benz
console.log(cars); //["Toyota", "Acura", "Lincoln", "Honda", "Subaru"]
//shift, removes the first array element
console.log(cars.shift()); //Toyota
console.log(cars); //["Acura", "Lincoln", "Honda", "Subaru"]
//unshfit, add element as the first array element
console.log(cars.unshift("Infiniti"));
console.log(cars); //["Infiniti", "Acura", "Lincoln", "Honda", "Subaru"]
//remove element
delete cars[1];
console.log(cars); //["Infiniti", empty, "Lincoln", "Honda", "Subaru"]
//splice, add items
cars.splice(2, 1, "LandRover"); //insert element at index 2, remove 1 element, insert the rest of the elements
console.log(cars); //["Infiniti", empty, "LandRover", "Honda", "Subaru"]
//isArray
console.log(Array.isArray(cars)); //true
console.log(cars instanceof Array); //true
//toString
console.log(cars.toString())
//join
console.log(cars.join("-"));
//concatenate
var a_1 = [1, 2, 3, 4];
var a_2 = ['Buick', 'Lincoln', 'Honda'];
var a_3 = a_1.concat(a_2);
console.log(a_3); //[1, 2, 3, 4, "Buick", "Lincoln", "Honda"]
//slice
var a_4 = a_3.slice(1, 5); //end index is exclusive
console.log(a_4); //[2, 3, 4, "Buick"]
//indexOf, returns the position of the first occurrence
console.log(a_3.indexOf("Honda")); //6
//lastIndexOf, returns the position of the last occurrence
var fruits = ["Apple", "Orange", "Apple", "Mango"];
console.log(fruits.lastIndexOf("Apple")); //2
Sort
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
console.log(fruits); //["Apple", "Banana", "Mango", "Orange"]
fruits.reverse();
console.log(fruits); //["Orange", "Mango", "Banana", "Apple"]
var a = [40, 100, 1, 5, 25, 10];
a.sort();
console.log(a); //[1, 10, 100, 25, 40, 5]
a.sort(function(a, b){return a - b}); //[1, 5, 10, 25, 40, 100]
console.log(a);
Max and Min
var a = [40, 100, 1, 5, 25, 10];
console.log(Math.max.apply(null, a));
console.log(Math.min.apply(null, a));
ForEach
var a = [40, 100, 1, 5, 25, 10];
function f(value)
{
console.log(value)
}
a.forEach(f);
Map
performing a function on each array element
the function takes 3 arguments, item value, item value, array itself
var a = [40, 100, 1, 5, 25, 10];
function f(value, index, array)
{
return value*2;
}
var b = a.map(f);
console.log(b);
Filter
creates a new array with array elements that passes a test
the function takes 3 arguments, item value, item value, array itself
var a = [40, 100, 1, 5, 25, 10];
function f(value, index, array)
{
if (value%2 == 0)
return value;
}
var b = a.filter(f);
console.log(b);
Reduce
runs a function on each array element to produce (reduce it to) a single value
the function takes 4 arguments, total value, item value, item value, array itself
var a = [40, 100, 1, 5, 25, 10];
function f(total, value, index, array)
{
return total + value;
}
var b = a.reduce(f);
console.log(b);
Every
check if all array values pass a test
the function takes 3 arguments, item value, item value, array itself
var a = [40, 100, 1, 5, 25, 10];
function f(value, index, array)
{
return value % 2 == 0;
}
console.log(a.every(f)); //false
Some
check if some array values pass a test
the function takes 3 arguments, item value, item value, array itself
var a = [40, 100, 1, 5, 25, 10];
function f(value, index, array)
{
return value % 2 == 0;
}
console.log(a.some(f)); //true
Find
returns the value of the first array element that passes a test function
the function takes 3 arguments, item value, item value, array itself
var a = [40, 100, 1, 5, 25, 10];
function f(value, index, array)
{
return value % 2 != 0;
}
console.log(a.find(f)); //1
FindIndex
returns the index of the first array element that passes a test function
the function takes 3 arguments, item value, item value, array itself
var a = [40, 100, 1, 5, 25, 10];
function f(value, index, array)
{
return value % 2 != 0;
}
console.log(a.findIndex(f)); //2
Destructuring Assignment
var [firstResort] = ["Kirkwood", "Squaw", "Alpine"];
console.log(firstResort); // Kirkwood
var [,,thirdResort] = ["Kirkwood", "Squaw", "Alpine"];
console.log(thirdResort); // Alpine
Spread Operator
Combine the contents of arrays
var peaks = ["Tallac", "Ralston", "Rose"];
var canyons = ["Ward", "Blackwood"];
var tahoe = [...peaks, ...canyons];
console.log(tahoe.join(', ')); // Tallac, Ralston, Rose, Ward, Blackwood
Extract element without changing the original array
var peaks = ["Tallac", "Ralston", "Rose"];
var [last] = [...peaks].reverse();
console.log(last); // Rose
console.log(peaks.join(', ')); // Tallac, Ralston, Rose, not change the order of elements in peaks
Get the rest of the array
var lakes = ["Donner", "Marlette", "Fallen Leaf", "Cascade"]
var [first, ...rest] = lakes
console.log(rest.join(", ")) // "Marlette, Fallen Leaf, Cascade"