Arrays In Javascript
var cars = ["Saab", "Volvo", "BMW"];
var name = cars[0]; //ACCESS ARRAY ELEMENT saab
cars[0] = "Opel"; //CHANGE ARRAY
console.log(cars); //Saab,volvo,BMW //FULL ARRAY ACCESS
cars.toString(); //ARRAY TO STRING
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop(); // Removes the last element ("Mango") from fruits and return also
fruits.push("Kiwi"); // Adds a new element ("Kiwi") to fruits and return new array length
fruits.shift(); // Removes the first element "Banana" from fruits same as pop
fruits.unshift("Lemon"); // Adds a new element "Lemon" to fruits at first pos
delete fruits[0]; // Changes the first element in fruits to undefined
fruits.splice(2, 2, "Lemon", "Kiwi"); //fruits.splice(whereNewElementsAdds,removedElements, "add1element", "add2 ...");
var marr = arr1.concat(arr2,arr3,arr4); //concate arrays to new arr
fruits.sort(); // Sorts the elements of fruits alphabetically
fruits.reverse(); // Then reverse the order of the elements
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b}); //SORT NUMERIC ARRAY ACCENDING
ARRAY ITERATION
var txt="";
var numbers = [45, 4, 9, 16, 25];
numbers.forEach(myFunction);
function myFunction(value, index, array) {
txt = txt + value + "<br>";
}
ARRAY MAP CREATE NEW ARRAY: to perform function on each element
var numbers1 = [45, 4, 9, 16, 25];
var numbers2 = numbers1.map(myFunction);
function myFunction(value, index, array) {
return value * 2;
}
ARRAY FILTER: to perform tests
var numbers = [45, 4, 9, 16, 25];
var over18 = numbers.filter(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
array.indexOf(item, start)
array.lastIndexOf(item, start)