Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Arrays
- let arr = new Array();
- let arr = []; // mostly used
- // Declare array with some initial data:
- let fruits = ["Apple", "Orange", "Plum"];
- // GET element from array - use index
- let fruits = ["Apple", "Orange", "Plum"];
- let apple = fruits[0]; // Apple
- let orange = fruits[1]; // Orange
- let plum = fruits[2]; // Plum
- // Index is a number starts from 0
- // --0-- --1-- --2--
- // ["Apple", "Orange", "Plum"]
- // UPDATE elements in array - use index
- fruits[2] = "Pear"; // now ["Apple", "Orange", "Pear"]
- // ADD new element to array - use index
- fruits[3] = "Lemon"; // now ["Apple", "Orange", "Pear", "Lemon"]
- // Arrays can contain ANY types inside, even mix of many types:
- let arr = [
- "Apple",
- { name: "John" },
- true,
- function() {
- alert("hello");
- }
- ];
- // get the object at index 1 and then show its name
- let n = arr[1]; // John
- // get the function at index 3 and run it
- arr[3](); // hello
- // use isArray to check if your variable is array:
- let arr = [1, 2, 3, 4, 5];
- console.log(Array.isArray(arr)); // true
- // ***************************************************************************
- // pop/push, shift/unshift
- // pop - take away last element
- let fruits = ["Apple", "Orange", "Pear"];
- console.log(fruits.pop()); // remove "Pear" and print it
- console.log(fruits); // Apple, Orange
- // push - add element to the end of array
- let fruits = ["Apple", "Orange"];
- fruits.push("Pear");
- console.log(fruits); // Apple, Orange, Pear
- // shift - take away first element
- let fruits = ["Apple", "Orange", "Pear"];
- console.log(fruits.shift()); // remove Apple and print it
- console.log(fruits); // Orange, Pear
- // unshift - add element to begining
- let fruits = ["Orange", "Pear"];
- fruits.unshift("Apple");
- console.log(fruits); // Apple, Orange, Pear
- // You can add multiple elements at once
- let fruits = ["Apple"];
- fruits.push("Orange", "Peach");
- fruits.unshift("Pineapple", "Lemon");
- // ["Pineapple", "Lemon", "Apple", "Orange", "Peach"]
- console.log(fruits);
- // ***************************************************************************
- // Arrays are Objects:
- let fruits = []; // make an array
- fruits[100] = 5; // assign a property with the index far greater than its length
- fruits.age = 25; // create a property with an arbitrary name
- // Special property - length
- // Represents number of items in array:
- let fruits = [];
- console.log(fruits.length); // 0
- fruits.push("a", "b", "c");
- console.log(fruits.length); // 3
- // Do not be confused with length\index!
- console.log(fruits[2]); // "c"
- // UNSAFE!
- // You can modify value of length:
- let fruits = [];
- console.log(fruits.length); // 0
- fruits.push("a", "b", "c");
- console.log(fruits.length); // 3
- fruits.length = 1000;
- console.log(fruits.length); // 1000
- fruits.push("d");
- console.log(fruits.length); // 1001
- // ***************************************************************************
- // Iterating (loops) for array
- let fruits = ["Apple", "Orange", "Pear"];
- // (1) Just regular 'for' loop with checking length
- for (let i = 0; i < fruits.length; i++) {
- console.log(fruits[i]);
- }
- // (2) for/of - special form for iterables(arrays)
- let fruits = ["Apple", "Orange", "Plum"];
- // for..of doesn’t give access to the index of the current element
- for (let fruit of fruits) {
- console.log(fruit);
- }
- // (!) Do NOT use for/in loop (as for iterating over object properties)
- let arr = ["Apple", "Orange", "Pear"];
- for (let key in arr) {
- console.log(arr[key]); // Apple, Orange, Pear
- }
- // because for..in iterates over ALL properties, not just indexes
- arr["some"] = "oops!";
- for (let key in arr) {
- console.log(arr[key]); // Apple, Orange, Pear, oops!
- }
- // (3) forEach method, runs provided function for every member of array
- let arr = ["Apple", "Orange", "Pear"];
- arr.forEach(function(item, index, array) {
- console.log(item);
- });
- // The result of the function (if it returns any) is thrown away and ignored.
- let res = arr.forEach(function(item, index, array) {
- return "NEW" + item;
- });
- console.log(arr)
- console.log(res)
- // (4) .map() method
- let arr = [10, 30, 100];
- let newArr = arr.map(function(item, index, array) {
- console.log(item);
- return item + 10;
- });
- console.log(newArr);
- // The result of the function will be passed to new array
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement