JAVASCRIPT

  minte9
REMEMBERS




Last update:   27-01-2022

Order

An array is a special data structure, to store ordered collections.
 
/**
 * Arrays are used to store ordered collections.
 * 
 * Objects are for storing key:value collections.
 * With object we can't insert new property between existing ones.
 * 
 * An array can store elements of any type.
 */

let vars = [
    "Apple", 
    "Orange", 
    () => {
        console.log('Hello World');
    },
    {name: 'John'}, // comma allowed
];
console.log(vars[1]);       // Orange
console.log(vars[3].name);  // John
vars[2]();                  // Hello World


/**
 * pop - extract last
 * push - append at the end of array
 * shift - extract first
 * unshift - add to begining of array
 * pop/push - also return the element
 */
vars = ["Apple", "Orange"];

vars.pop();
console.log(vars); // [Apple]

vars.push("Pear");
console.log(vars); // [Apple, Pear]

vars.shift();
console.log(vars); // [Pear]

vars.unshift("Orange", "Lemon");
console.log(vars); // [Orange, Lemon, Pear]

Object

An array is an object and behaves like an object.
 
/**
 * There are only eight data types in Javascript, and ...
 * an array is just a special type of object.
 * 
 * The engine stores elements in order, to work realy fast.
 * 
 * The square brackets arr[0] come from object syntax.
 * An array is copied by reference (just like objects).
 */

let A = ['Banana'];

let B = A;
console.log(A === B); // true

B.push("Orange");
console.log(A); // [Banana, Orange]

Matrix

Arrays can have items that are also arrays.
 
/**
 * Multidimensional arrays
 */

let matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
];
console.log(matrix[1][1]); // 5, central element


/**
 * Loops
 * 
 * The for ... of
 * is the prefered way to loop over items.
 * 
 * The for ... in
 * It iterate over all properties, not just numeric ones.
 * It is optimize for objects, it is 10-100 slower.
 */

for (let i=0; i<matrix.length; i++) { // classic loop
    console.log(matrix[0][i]); 
        // 1, 4, 7
}

for (let item of matrix) {
    console.log(item[0]); 
        // 1, 4, 7
}

for (let key in matrix) { // not recommended
    console.log(matrix[key][0]); 
        // 1, 4, 7
}

matrix.forEach((value, key, arr) => {
    console.log(value.toString());
        // 1, 2, 3
        // 4, 5, 6
        // 7, 8, 9
});

["Bilbo", "Gandalf"].forEach(console.log);
    // Bilbo 0 [ 'Bilbo', 'Gandalf', 'Gollum' ]
    // Gandalf 1 [ 'Bilbo', 'Gandalf', 'Gollum' ]

    
/**
 * Length
 * 
 * It is actually not the count of values in array, 
 * is the greatest numeric index plus one.
 */

let vars = ["Orange"];
vars[123] = "Apple";

console.log(vars.length); // 124 - Look Here

Methods

Most useful and often used (filter, map, sort).
 
/**
 * Array methods: filter, map, sort
 */

let A = [
    {id: 1, user: 'John'},
    {id: 2, user: 'Pete'},
    {id: 3, user: 'Marry'},
];
let B = A.filter(item => item.id < 2);
console.log(B); 
    // {id: 1, user: 'John'}

let C = ["Bilbo", "Gandalf"];
let D = C.map(item => item.length);
console.log(D); 
    // [5, 7]

let E = [
    {id: 12, user: 'John'},
    {id: 2, user: 'Pete'},
    {id: 3, user: 'Marry'},
];
E.sort((a, b) => a.id - b.id);
console.log(E[0]); 
    // {id: 2, user: 'Pete'},

Questions     github Types
Json
        A B C D E F
🔔
1/3