array.html /////////////////// array.js function example1(){ // create new arrays var a = []; var sparse = [0, 1.1, , true, , "x", , {z:4, r:7}]; // creates sparse array var empty = new Array(); // creates empty array var array = new Array(5); // single argument, represents length var array2 = new Array(1, 2, 3, 4, 5); // [1 2 3 4 5] console.log(sparse[6]); // undefined console.log(sparse[-1]); // undefined, "-1" treated as object property // not as array index, which is non-existent console.log(sparse.length); // 8 console.log(empty.length); // 0 console.log(a.length); // 0 console.log(array.length); // 5 console.log(array2.length); // 5 } function example2(){ var array = [1,2,3,4,5] // Read and write to arrays console.log("---------------"); array[5] = 6; // [1 2 3 4 5 6] console.log(array.length); // 6 array["10"] = 11; // set element at index 10 with value 11 // "10" behaves like array index and not as object // property console.log(array.length); // length = 11, only 7 elements in array // sparse arrays console.log("----------"); var a = new Array(5); // length 5 and no elements var a1 = [,,]; // length = 2 with no elements var a2 = [undefined,,]; // length = 2 with 1 undefined element console.log(a1.length); // 2 console.log(a2.length); // 2 console.log(0 in a1); // false, a1 has no element at 0 console.log(0 in a2); // true, a2 has element undefined at 0 // array.length var b = [1,2,3,4,5]; // [1,2,3,4,5] b.length = 3; // [1,2,3] b.length = 0; // delete all elements b.length = 5; // length = 5, no elements // add and delete elements from an array console.log("-----------------"); var c = []; c.push(1,2,3); console.log(c); // [1, 2, 3] delete c[1]; console.log(c) // [1, , 3] sparse array console.log(1 in c); // false console.log(c.length); // 3 } function example3(){ var a = [1,2,3,4,5]; // non-sparse var array = [1,null,3,undefined,5, , 7, null, , 8]; // sparse array var i; // index variable console.log("---------------"); for (i in a) // printing non-sparse array console.log(a[i]); console.log("---------------"); for(i = 0; i < array.length; i++){ // looping sparse array if(!array[i]) continue; // skip nulls, undefined and non-existent console.log(array[i]); } // Multidimensional array console.log("--------------"); var table = new Array(5); // 5 rows for(i = 0; i < table.length; i++) table[i] = new Array(5); // 5 columns for(var row = 0; row < table.length; row++){ for(var col = 0; col < table[row].length; col++){ table[row][col] = row * col; } } console.log(table[2][3]); // 6 } function example4(){ // join, converts elements to strings,concatenates, returns string var a = [1,2,3,4,5]; console.log(a.join()) console.log("-----------------") // reverse, reverses elements of array and returns reversed array // modifies array console.log(a.reverse()) console.log("----------------") // sort(), sorts elements in alphabetical order, case-sensitive // modifies array // sort(), sorts non-alphabetically // comparison function: decides on 2 element comparison // if first > second, return > 0 // if first < second, return < 0 // if first == second, return 0 var fruits = ["apple", "orange", "Banana"]; console.log(fruits.sort()); console.log(fruits.sort(function(s, t){ var a = s.toLowerCase(); var b = t.toLowerCase(); if (a < b) return -1; if (a > b) return 1; return 0; })) // concat, returns new array with concatenated elements // returns new concatenated array var x = [1,2,3]; // [1,2,3] var y = x.concat([4,5,[6,7]]); // [1,2,3,4,5,[6,7]] console.log("-------------"); // slice(x,y), returns subarray [x,y), does not modify array // x : start index, inclusive // y : end index, exclusive // index:[ 0, 1, 2, 3, 4] // index:[-5, -4, -3, -2, -1] var z = [ 1, 2, 3, 4, 5]; console.log(z.slice(0,3)); // [1,2,3] console.log(z.slice(3)); // [4,5] console.log(z.slice(1,-1)); // [2,3,4] console.log(z.slice(-3,-2)); // [3] console.log("------------------------"); // splice(x,y), x:deleting index, y:number of elements to delete // modifies the array, moves elements over to the left var f = [1,2,3,4,5,6,7,8]; console.log(f.splice(4)); // Returns [5,6,7,8], f is [1,2,3,4] console.log(f.splice(1,2)); // Returns [2,3], f is [1,4] console.log(f.splice(1,1)); // Returns [4], f is [1] // push()->append at the end of array // pop()->deletes at the end of array // modify the array in place var stack = []; stack.push(1,2) // [1,2] stack.pop(); // [1] stack.push(3); // [1,3] stack.pop(); // [1] stack.push([4,5]) // [1,[4,5]] stack.pop(); // [1] stack.pop(); // [] // shift->delete at the beginning, shifts elements to the left // unshift->insert at the beginning, shifts elements to the right var h = []; h.unshift(1); // [1] h.unshift(2); // [2,1] h.shift(); // [1] h.unshift(3,[4,5]); // [3,[4,5],1] h.shift(); // [[4,5],1] h.shift(); // [1] h.shift(); // [] // toString()->returns string with array elements console.log("--------------------"); console.log([1,2,3].toString()); // '1,2,3' console.log([1,[2,'c']].toString()); // '1,2,c' console.log("--------------"); // indexOf()->returns the index for the first occurrence of an element // lastIndexOf()->returns the index for last occurrence of an element var g = [0,3,2,0,1,2,3,5,6,3]; console.log(g.indexOf(5)); // 7 console.log(g.lastIndexOf(3)); // 9 ///////////////////////////////////////// console.log("---Array methods----"); // forEach, map, filter, every, some, reduce, reduceRight // First argument is a function and invoke that function once for each // element of the array // function takes 3 arguments: // 1st is the value of each element in array // 2nd, is the value of this keyword, // 3rd is return value ////////////////////////////////////////// // forEach var array = [1,2,3,4,5]; var sum = 0; array.forEach(function(value) {sum += value}); console.log(sum); // 15 // map var m = array.map(function(x) {return x * x}); console.log(m) // [1,4,9,16,25] // filter var smallerthan3 = array.filter(function(x) { return x < 3 }); console.log(smallerthan3); // [1,2] // every and some, returns true or false console.log(array.every(function(x) {return x < 10})); // true, all <10 console.log(array.some(function(x) { return x%2 === 0 })); // true, some even // reduce->combine elements and produce single value // reduce(previousValue,currentValue,index,array), initial value(optional) console.log([0,1,2,3,4,5].reduce(function(x,y) {return x+y}, 0)) // 15=0+15 // reduceRight(previousValue, currentValue, index, array), initial value (optional) console.log([0,1,2,3,4].reduceRight(function(x,y) {return x+y})) // previousValue = 4, currentValue = 3, index = 3 // previousValue = 7, currentValue = 9, index = 2 // previousValue = 9, currentValue = 1, index = 1 // previousValue = 10,currentValue = 0, index = 0 // Return 10 console.log([0,1,2,3,4].reduceRight(function(x,y) {return x+y}, 10)); // previousValue = 10, currentValue = 4, index = 4 // previousValue = 14, currentValue = 3, index = 3 // previousValue = 17, currentValue = 2, index = 2 // previousValue = 19, currentValue = 1, index = 1 // previousValue = 20,currentValue = 0, index = 0 // Return 20 }