Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. // Arrays
  2.  
  3. let arr = new Array();
  4. let arr = []; // mostly used
  5.  
  6. // Declare array with some initial data:
  7. let fruits = ["Apple", "Orange", "Plum"];
  8.  
  9. // GET element from array - use index
  10. let fruits = ["Apple", "Orange", "Plum"];
  11.  
  12. let apple = fruits[0]; // Apple
  13. let orange = fruits[1]; // Orange
  14. let plum = fruits[2]; // Plum
  15.  
  16. // Index is a number starts from 0
  17. // --0-- --1-- --2--
  18. // ["Apple", "Orange", "Plum"]
  19.  
  20. // UPDATE elements in array - use index
  21. fruits[2] = "Pear"; // now ["Apple", "Orange", "Pear"]
  22.  
  23. // ADD new element to array - use index
  24. fruits[3] = "Lemon"; // now ["Apple", "Orange", "Pear", "Lemon"]
  25.  
  26. // Arrays can contain ANY types inside, even mix of many types:
  27. let arr = [
  28. "Apple",
  29. { name: "John" },
  30. true,
  31. function() {
  32. alert("hello");
  33. }
  34. ];
  35.  
  36. // get the object at index 1 and then show its name
  37. let n = arr[1]; // John
  38. // get the function at index 3 and run it
  39. arr[3](); // hello
  40.  
  41. // use isArray to check if your variable is array:
  42. let arr = [1, 2, 3, 4, 5];
  43. console.log(Array.isArray(arr)); // true
  44.  
  45. // ***************************************************************************
  46. // pop/push, shift/unshift
  47.  
  48. // pop - take away last element
  49. let fruits = ["Apple", "Orange", "Pear"];
  50. console.log(fruits.pop()); // remove "Pear" and print it
  51. console.log(fruits); // Apple, Orange
  52.  
  53. // push - add element to the end of array
  54. let fruits = ["Apple", "Orange"];
  55. fruits.push("Pear");
  56. console.log(fruits); // Apple, Orange, Pear
  57.  
  58. // shift - take away first element
  59. let fruits = ["Apple", "Orange", "Pear"];
  60. console.log(fruits.shift()); // remove Apple and print it
  61. console.log(fruits); // Orange, Pear
  62.  
  63. // unshift - add element to begining
  64. let fruits = ["Orange", "Pear"];
  65. fruits.unshift("Apple");
  66. console.log(fruits); // Apple, Orange, Pear
  67.  
  68. // You can add multiple elements at once
  69. let fruits = ["Apple"];
  70. fruits.push("Orange", "Peach");
  71. fruits.unshift("Pineapple", "Lemon");
  72. // ["Pineapple", "Lemon", "Apple", "Orange", "Peach"]
  73. console.log(fruits);
  74.  
  75. // ***************************************************************************
  76. // Arrays are Objects:
  77.  
  78. let fruits = []; // make an array
  79. fruits[100] = 5; // assign a property with the index far greater than its length
  80. fruits.age = 25; // create a property with an arbitrary name
  81.  
  82. // Special property - length
  83. // Represents number of items in array:
  84. let fruits = [];
  85. console.log(fruits.length); // 0
  86. fruits.push("a", "b", "c");
  87. console.log(fruits.length); // 3
  88.  
  89. // Do not be confused with length\index!
  90. console.log(fruits[2]); // "c"
  91.  
  92. // UNSAFE!
  93. // You can modify value of length:
  94. let fruits = [];
  95. console.log(fruits.length); // 0
  96. fruits.push("a", "b", "c");
  97. console.log(fruits.length); // 3
  98. fruits.length = 1000;
  99. console.log(fruits.length); // 1000
  100. fruits.push("d");
  101. console.log(fruits.length); // 1001
  102.  
  103. // ***************************************************************************
  104. // Iterating (loops) for array
  105.  
  106. let fruits = ["Apple", "Orange", "Pear"];
  107.  
  108. // (1) Just regular 'for' loop with checking length
  109. for (let i = 0; i < fruits.length; i++) {
  110. console.log(fruits[i]);
  111. }
  112.  
  113. // (2) for/of - special form for iterables(arrays)
  114. let fruits = ["Apple", "Orange", "Plum"];
  115. // for..of doesn’t give access to the index of the current element
  116. for (let fruit of fruits) {
  117. console.log(fruit);
  118. }
  119.  
  120. // (!) Do NOT use for/in loop (as for iterating over object properties)
  121. let arr = ["Apple", "Orange", "Pear"];
  122. for (let key in arr) {
  123. console.log(arr[key]); // Apple, Orange, Pear
  124. }
  125. // because for..in iterates over ALL properties, not just indexes
  126. arr["some"] = "oops!";
  127. for (let key in arr) {
  128. console.log(arr[key]); // Apple, Orange, Pear, oops!
  129. }
  130.  
  131. // (3) forEach method, runs provided function for every member of array
  132. let arr = ["Apple", "Orange", "Pear"];
  133. arr.forEach(function(item, index, array) {
  134. console.log(item);
  135. });
  136. // The result of the function (if it returns any) is thrown away and ignored.
  137. let res = arr.forEach(function(item, index, array) {
  138. return "NEW" + item;
  139. });
  140. console.log(arr)
  141. console.log(res)
  142.  
  143. // (4) .map() method
  144. let arr = [10, 30, 100];
  145. let newArr = arr.map(function(item, index, array) {
  146. console.log(item);
  147. return item + 10;
  148. });
  149. console.log(newArr);
  150. // The result of the function will be passed to new array
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement